我想创建一个MySQLi查询,从数据库中结构不同但有一个公共列的两个表中进行选择。
例如:
table one:
id / post / date
1 / hi man / 13 - 11 - 2013
table two:
id / shared post / date
1 / hey / 12 - 11 - 2013
我想让查询看起来像这样:
hey / 12 - 11 - 2013
hi man / 13 - 11 - 2013
有人能帮我吗?
使用UNION:
UNION用于将多个SELECT语句的结果组合为一个结果集。
因此:
SELECT post, date FROM tableone
UNION SELECT sharedpost, date FROM tabletwo
ORDER BY date
这将类似于:
SELECT * FROM x, y WHERE x.post = y.shared_post ORDER BY x.post;
您应该更改查询以匹配您的表结构。
SELECT * FROM x INNER JOIN y ON x.id=y.id ORDER BY x.date ASC;
你想要这个?
编辑:也许你知道这个:
SELECT * FROM (SELECT * FROM x UNION SELECT * FROM y) ORDER BY 1 ASC ;