...

View Full Version : How to do this effect



xiaodao
01-21-2010, 06:10 PM
$sql=mysql_query("select * from abc WHERE name='laal'");

$r=mysql_fetch_array($sql);

$id=$r['id'];

mysql_query("SELECT * from cde WHERE id='$id'");

how to combine these two query together to become one query

kbluhm
01-21-2010, 06:23 PM
No idea what your tables look like... whether `name` is unique, etc. There are multiple options.

http://dev.mysql.com/doc/refman/5.1/en/subqueries.html


SELECT *
FROM `cde`
WHERE `id` = (
SELECT `id`
FROM `abc`
WHERE `name` = 'laal'
)


http://dev.mysql.com/doc/refman/5.1/en/join.html


SELECT `cde`.*
FROM `cde`
LEFT JOIN `abc`
ON `abc`.`id` = `cde`.`id`
WHERE `abc`.`name` = 'laal'

xiaodao
01-21-2010, 06:46 PM
left join? table cde have 2 records which are same id, which means the id from abc will be used to select two recrods from table cde, i think left join cannot produce this outcome right?

kbluhm
01-21-2010, 06:50 PM
I think it would.

The sub query won't work for you? It is technically still two queries but will result in less code.

The left join is what you're looking for. It will return all matching rows from `cde` where `cde`.`id` = `abc`.`id` and `abc`.`name` = laal

xiaodao
01-21-2010, 06:57 PM
please help me again. Let me give you the table structure

table abc

cid name quality
1 laal 3
2 gaga 4

table cde
id cid subname
1 1 baba
2 1 sasa
3 2 tata

the cid in table cde is same as cid in table abc

now i want to ge4t something like structure below
cid name subname quality
1 laal (baba,sasa) 3
2 gaga (tata) 4


how can i do this?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum