简体   繁体   English

我可以从基于多个其他表的表中获取数据吗?

[英]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. 基本上,我想根据节省量从table1中获取数据,并使用它从表2中获取数据,并使用该数据从表3中获取所有信息。但这将无法工作。 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. 我也无法创建新表,我只想显示表3中的数据。

Use join 使用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";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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