简体   繁体   中英

Can I get data from a table based on multiple other tables?

$query = "SELECT * FROM table3 WHERE name_id = '(SELECT name_id FROM table2
WHERE salary < 1000 && name = '(SELECT name FROM table1
WHERE savings > 1000)')'";

Basically I want to get the data from the table1 based on the savings and use it to get the data from table 2 and use that data to get all the information from table 3. But this wont work. Is my code right or am I doing something wrong?

I also cannot create new tables, I simply want to display the data from table 3.

Use join

SELECT * FROM table3 t3 join table2 t2 
on t3.name_id=t2.name_id
join table1 t1
on t3.name=t1.name
where salary < 1000 and savings > 1000
$query="SELECT * FROM table3 LEFT JOIN table2 ON    table3.name_id=table2.name_id
LEFT JOIN table1 ON table3.name=table1.name 
WHERE table2.salary < 1000  AND table1.savings > 1000 "

Another syntax of join is

SELECT * FROM table1 t1,table2 t2 ,table3 t3 
where t1.name = t3.name and 
t2.name_id = t3.name_id and 
t1.savings > 1000 and t2.salary < 1000;
$query = "SELECT t3.* FROM table3 t3 
INNER JOIN table2 t2 ON t2.name_id = t3.name_id AND t2.salary < 1000
INNER JOIN table1 t1 ON t1.name = t2.name AND t1.savings > 1000";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM