ice_php — 2003-03-03T15:35:06-05:00 — #1
i want to select some rows from my database from multiple tables
i use this code:
$sql = mysql_query("SELECT * FROM $mulchtable, $snipetable, $conctable, $rjumptable WHERE winner = '$name' ORDER BY date DESC LIMIT 5");
Will this code select 5 rows from all 4 tables where the date is newest
( because i havent tested it, it may be ASC becasue i get confused sometimes ) or will it only select 5 from 1 table ?
If it selects from just the one table is there an easier way of selecting
from multiple tables ?
freakysid — 2003-03-03T16:32:29-05:00 — #2
You really need to read up on how joins work. I don't think you will be happy with the results of that query. The idea of a relational database is that there are relationships between the tables ("relations") in the table.
You probably need to specify on which keys to join the tables together.
FROM table1, table2, table3
WHERE table1.value = table2.foreignkey
AND table2.value = table3.foreignkey
Get the idea?
ice_php — 2003-03-03T16:49:38-05:00 — #3
im quite flouent in PHP but only know some VV basic MySQL
i dont get the idea of a foreignkeys ?
what are they ?
r937 — 2003-03-03T21:18:07-05:00 — #4
foreign keys are the values that relate rows in different tables
here's a very simple example
consider categories -- catid is the primary key
now here's another table -- thingid is the primary key, and catid is the foreign key
thingid thingdesc catid
104 quartz 14
105 tomato 13
106 cat 12
107 cow 12
108 granite 14
109 naked molerat 12
110 copper 14
foreign keys reference primary keys
in most databases (not mysql), you cannot enter a row with a value in the foreign key that does not exist in the primary key
you could not, for example, enter
111 sandwich 15
becaase 15 isn't in the categories table
you don't have to check it -- the database does it for you (again, not mysql)
if you want more, check some of the links on my site --