似乎无法想出这一个。我想将三个不同表中的数据放入我页面中的一个表中。
我的桌子
People
+----+------+--------+------+
| id | name | status | date |
+----+------+--------+------+
House
+----+---------+--------+------+
| id | address | status | date |
+----+---------+--------+------+
Alarms
+----+------+--------+------+
| id | type | status | date |
+----+------+--------+------+
我想从状态为2的那三个表中获取所有数据。我怎么能这样做并将它们放在按日期排序的一个html表中。
如果有人能指出正确的方向,那就太棒了。
答案 0 :(得分:0)
试试这个
SELECT p.id,
p.name,
p.status,
p.date,
h.address,
a.type
FROM people p
INNER JOIN House h ON p.id = h.id //this i just guessed the relation you should make your relation here
INNER JOIN Alarms a ON h.id = a.id //this i just guessed the relation you should make your relation here
WHERE p.status =2
ORDER BY p.date
如果你的表没有关系,那就试试这个
select id,'type' as type,name,'adress' as adress,status,date from people where status =2
union
select id,'type' as type,name,adress,status,date from House where status =2
union
select id,type,name,'adress' as adress,status,date from people where status =2