nzrobert — 2011-03-08T00:32:44-05:00 — #1
Hi there at the moment i am using 2 select statements. a) to pull out all the product id's where a manufacturer id = $_GET..... and b) to pull out all the product names out of a language table where the product id = y (what ever i got out of the first statement). This is the only way i was able to get around the problem i had. The issue is that the second statement is in a while loop and i can't order the results alphabetically.
Here is the code:
$r3 = mysql_query("SELECT `id_product` FROM `pl_product` WHERE id_manufacturer='".$_GET['id_manufacturer']."'");
while ($row = mysql_fetch_object($r3))
$prod_id = $row->id_product;
$r4 = mysql_query("SELECT `name` FROM pl_product_lang WHERE id_product='".$prod_id."' AND id_lang = 1 ORDER BY `name` ASC");
while ($row = mysql_fetch_object($r4))
$prod_name = $row->name;
$manu_id = $_GET['id_manufacturer']
<option value="<?php echo $manu_id;?>"><?php echo $prod_name; ?></option>
I know there is a single statement i can use, presumeably with a JOIN, i just can't seem to get it working... this is what i have:
$r3 = mysql_query("SELECT `pl_product.id_product`, `pl_product_lang.name` FROM `pl_product` WHERE `pl_product.id_manufacturer`='".$_GET['id_manufacturer']."' INNER JOIN `pl_product_lang` ON `pl_product.id_product` = `pl_product_lang.id_product`");
while ($row = mysql_fetch_object($r3))
$prod_name = $row->pl_product_lang.name;
$manu_id = $_GET['id_manufacturer'];
Please point me in the right direction.
Thanks in advance.
nzrobert — 2011-03-08T02:07:37-05:00 — #2
Ok to put is simpler how do i change these statements in to one statement:
`id_manufacturer` = '".$_GET['id_manufacturer']."'"
`id_product` = '".$prod_id."'
($prod_id is the result of the query #1)
nzrobert — 2011-03-08T02:29:09-05:00 — #3
samanime — 2011-03-08T02:52:30-05:00 — #4
Just so you know, you don't really have to bump around here... we're pretty good at getting to posts even if they sink down a bit.
For simplicity sake, this example changes your get to $manufacturer (be sure to clean this input before you run the query or there could be trouble).
To join them with a LEFT JOIN (which is probably what you want), you'd do something like this:
SELECT p.id_product, l.name
FROM pl_product p
LEFT JOIN pl_product_lang l
ON p.id_product = l.id_product
WHERE id_manufacturer = '$manufacturer' AND id_product = '$prod_id'
Wrap that in "" and you won't need to escape them and use string concatenator (.) and all that ("" process simple variable names in them automatically, very handy).
So, basically to do a join you start with a normal query. Then you figure out what table(s) you want to join. You then specify them in the join (LEFT JOIN is the most common, though there are about a dozen different types which you can check out in MySQL documentation). After that, you then specify an ON condition, which specifies which value in table A matches a value in table B.
After that everything else is pretty much the same.
nzrobert — 2011-03-08T04:02:54-05:00 — #5
Excellent, thanks.. works a charm.
Am i right in thinking that those letters can be anything you decide them to be?
and you simply define which table each letter refers to after you mention it in your query?
guido2004 — 2011-03-08T04:08:03-05:00 — #6
They are called aliasses. You must use them when you join the same table twice in one query, and you may use them in all other cases (for example to avoid having to write long table names multiple times in 1 query).
And yes, they can be almost anything you want.
nzrobert — 2011-03-08T04:11:27-05:00 — #7
Cheers mate, your a legend!
guido2004 — 2011-03-08T04:17:20-05:00 — #8
You must use an alias in this case as well
) [B]AS b[/B]
ON table1.id = b.id
guelphdad — 2011-03-08T08:06:16-05:00 — #9
I agree with that. nzrobert, you ESPECIALLY should not be bumping your posts after 20 minutes.