473,788 Members | 2,897 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

mysql/php query

I have a question about (i think) joining.

If I have a table in a database that has this info:

key - name - favorite
1 - john - 2
2 - judy - 3
3 - joe - 1

the favorite icecream table is this:
key - flavors
1 - vanilla
2 - chocolate
3 - strawberry

how do i do a query to display that judy's favorite is strawberry.

obviously this is a simple example. i am doing working on something
that is much more complex than this, but if anyone can give a hint i
can apply to the thing i am working on.

Thanks in advance!
Jul 17 '05
13 5558
Andy Hassall (4.980% quality rating):

OK, but I'd argue that if you needed an outer join in this specific
case, then the database is broken; referential integrity checks
really belong in the database (although the real world sometimes gets
in the way of that).
I'm not sure what you're saying here about referential integrity checks
belonging in the database. In the simple example given, if someone is
added to the people table, but no corresponding entry for them is ever
added to the favorites table, how does the database know whether or not
that is a problem (and what should it do if it is?).
Well, It Depends. But any time you fetch more data than you need, there's a
difference. And once you get past trivial queries, using outer joins where
they're not needed can certainly change for the worse
I frequently use them in more complex queries, even joining more than
two tables. However, in most cases where I want a LEFT JOIN, it's
because there may be no corresponding entries in the second (and
potentially third) table(s).

But still, there are cases where there should be no good reason for the
second table to be missing the corresponding entry, but I don't want to
omit output data if the entry is somehow missing. Like in the sample
code where I was checking "users LEFT JOIN user_settings". A user might
accidentally not have had his user_settings record created... but that
doesn't mean the user doesn't exist, and so if I am trying to show a
list of users with some piece of information from their user settings, I
will do a left join to make sure I get all the users. The alternative
(if I am not willing to trust the user_settings table to have all of the
users' entries) would seem to be to do a select on the users table and
then N selects on the user_settings table, but that's clearly less
efficient (N+1 queries vs. 1 query).
and constrain the access paths your database can use.


I don't know what this means at all.

/joe
--
The choad is wholesale. The 3LA is slimy and educational.
Jul 17 '05 #11
On Mon, 17 Nov 2003 21:27:35 +0000 (UTC), Disco Plumber
<sc**@moralmino rity.org> wrote:
Andy Hassall (4.980% quality rating):

OK, but I'd argue that if you needed an outer join in this specific
case, then the database is broken; referential integrity checks
really belong in the database (although the real world sometimes gets
in the way of that).
I'm not sure what you're saying here about referential integrity checks
belonging in the database. In the simple example given, if someone is
added to the people table, but no corresponding entry for them is ever
added to the favorites table, how does the database know whether or not
that is a problem (and what should it do if it is?).


But that can't happen in the data model given; favourite was part of the first
table.

The data given was:

key - name - favorite
1 - john - 2
2 - judy - 3
3 - joe - 1

the favorite icecream table is this:
key - flavors
1 - vanilla
2 - chocolate
3 - strawberry

Since 'favorite' was part of the 'person' row, then the only situations where
an outer join would apply would be:

(a) favorite was null (this could be a valid case for an outer join)
(b) favorite was set to a value that does not appear in the favorite table
(this was the case I was referring to as being broken, since I was assuming
'favorite' was declared as not null in the first table).

Case (b) should be caught by a foreign key constraint, and so on the
assumption that favorite is not null, there's no valid situation for an outer
join.
Well, It Depends. But any time you fetch more data than you need, there's a
difference. And once you get past trivial queries, using outer joins where
they're not needed can certainly change for the worse


I frequently use them in more complex queries, even joining more than
two tables. However, in most cases where I want a LEFT JOIN, it's
because there may be no corresponding entries in the second (and
potentially third) table(s).


Yes, that's the purpose of an outer join.
But still, there are cases where there should be no good reason for the
second table to be missing the corresponding entry, but I don't want to
omit output data if the entry is somehow missing. Like in the sample
code where I was checking "users LEFT JOIN user_settings". A user might
accidentally not have had his user_settings record created... but that
doesn't mean the user doesn't exist, and so if I am trying to show a
list of users with some piece of information from their user settings, I
will do a left join to make sure I get all the users. The alternative
(if I am not willing to trust the user_settings table to have all of the
users' entries) would seem to be to do a select on the users table and
then N selects on the user_settings table, but that's clearly less
efficient (N+1 queries vs. 1 query).
Sure, if it makes sense (or is convenient) to bring rows back where there is
no relation. On the other hand, if you're querying for a user's settings, it
doesn't necessarily make sense to bring back a row if there are no settings in
the table. That doesn't imply the user doesn't exist, just that that user has
no settings.

I see the point you're making (and even agree!), but what I'm trying (badly)
to get across was that the original question never called for an outer join,
just an ordinary join. There's no advantage to having it return one row with
the flavour field NULL, versus no rows indicating no favourites matched.

As you said on the other reply:
Of course, if I am
querying for information I'm not going to use (e.g., if I was going to
ignore those rows of the result where fields came back NULL), that is a
waste of processing.


Exactly! :-)

--
Andy Hassall (an**@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #12
While thinking about how cool Tipper Gore was, Andy Hassall blurted:

But that can't happen in the data model given; favourite was part of
the first table.
good point, I should have checked the initial example again before
spouting.
Case (b) should be caught by a foreign key constraint, and so on the
assumption that favorite is not null, there's no valid situation for an outer
join.
ah, so in a foreign key constraint I can tell the db "don't allow me to
add this person record if there is no matching flavor record for his
favorite"?
doesn't necessarily make sense to bring back a row if there are no settings in
the table. That doesn't imply the user doesn't exist, just that that user has
no settings.


yes, the example I cited was actually a combined "get all users" and
"get their settings" simultaneously, so it made sense to do the left
join.

/joe
--
In the emo garage, the chair from Cuddles the Cat will go to Psi U. Cuddles
the Cat's delightful computer from Hojohoro Bekahamu will go to Sarah H..
Mike Doyle ignores the monitor from Faff.
Jul 17 '05 #13
On Mon, 17 Nov 2003 22:59:36 +0000 (UTC), Disco Plumber
<sc**@moralmino rity.org> wrote:
Case (b) should be caught by a foreign key constraint, and so on the
assumption that favorite is not null, there's no valid situation for an outer
join.


ah, so in a foreign key constraint I can tell the db "don't allow me to
add this person record if there is no matching flavor record for his
favorite"?


Yes - it'll raise an error that there is no parent record.

Additionally it won't let you delete the flavour record leaving 'dangling'
rows in the people table that were referencing it; it'll complain there are
child records present. Typically you can set the constraint to either raise an
error, 'on delete cascade' (deleting the child rows too), or 'on delete set
null'.

--
Andy Hassall (an**@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3530
by: Lenz Grimmer | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, MySQL 4.0.14, a new version of the popular Open Source/Free Software Database, has been released. It is now available in source and binary form for a number of platforms from our download pages at http://www.mysql.com/downloads/ and mirror sites.
0
3948
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest version of this document can be found at: http://prdownloads.sourceforge.net/souptonuts/README_mysql.txt?download
1
2560
by: Cern | last post by:
Is it somebody out there who has made a migration from an Oracle server to an MySQL server?? The scenario is as simply: I've got a Oracle 8 server with a database with content that I want to transfer to a MySQL database. No special data, constraints etc that MySQL not will handle. My solution is to reverse engineer the database from ERStudio and then produce a SQL script that will insert the data into the MySQL engine. But I can't do...
1
3380
by: jlee | last post by:
I'm pretty much a newbie on mysql, and I need some help. I am running mysql Ver 12.22 Distrib 4.0.24, for portbld-freebsd5.4 (i386) on a server hosting an active website. The site's developer uses his own php shopping cart to receive customer orders. The configuration was done via cPanel with no external modifications - which produced no protests when built, ran and connected with no
1
2830
by: Good Man | last post by:
Hi there I've noticed some very weird things happening with my current MySQL setup on my XP Laptop, a development machine. For a while, I have been trying to get the MySQL cache to work. Despite entering the required lines to "my.ini" (the new my.cnf) through notepad AND MySQL Administrator, the cache does not work. So, today I took a peek at the 'Health' tab in MySQL Administrator.
3
6092
by: Juan Antonio Villa | last post by:
Hello, I'm having a problem replicating a simple database using the binary log replication, here is the problem: When the master sends an update to the slave, an example update reads as follows: UPDATE MainInfo SET dAddress='38 Holland Blvd', dCity='miami', dState='FL', dZip='33000', dCountry='USA', dPhone='999987565', dNum='AC15857', dName='Michael A Scott' WHERE did=22'
1
15393
by: Ike | last post by:
Recently, I began using a different MySQL verver (i.e. different machine as well as different version#, going from 4.12a to 4.1.9 max). The following query used to work: select firstname, lastname, from associates where username like 'nancianne' but now fails with: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from associates
3
8515
by: Me Alone | last post by:
Hello: I am trying to edit some C code I found in "The definitive guide to using, programming, and administering MySQL" by Paul DuBois. This C client program connects and then segfaults when the function load_image is called. Would anyone be able to point me to what I might be doing wrong? Thanks in advance, C Newbie
221
367743
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application needs to store entire files, the preferred method is to save the file onto the server’s file-system, and store the physical location of the file in your database. This is generally considered to be the easiest and fastest way to store files. ...
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10370
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10177
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8995
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5402
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.