简体   繁体   English

如何在SQL中合并两个查询的结果?

[英]How do I combine the results of two queries in SQL?

My bad if I don't understand simple things, as I am just beginning to write SQL queries. 如果我不懂简单的事情,那会很糟糕,因为我才刚刚开始编写SQL查询。

I have two queries: 我有两个查询:

SELECT * FROM status WHERE author IN (SELECT user1 FROM friends WHERE user2='$username' AND accepted='1') OR author IN (SELECT user2 FROM friends WHERE user1='$username' AND accepted='1') SELECT * FROM状态作者所在的位置(从好友中选择user1的用户,user2 ='$ username'并被接受='1')或作者处于作者状态(选择用户2,从朋友的位置,其中user1 ='$ username'并且被接受='1')

and

SELECT * FROM status WHERE author = '$username' SELECT * FROM status WHERE author ='$ username'

How can I combine the results of these two queries, either natively in the SQL query, or in PHP? 如何将这两个查询的结果组合在SQL查询或PHP中?

Try UNION : 尝试UNION

SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;

To allow duplicate values, use the ALL keyword with UNION. 若要允许重复的值,请将ALL关键字与UNION一起使用。

SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;

What's wrong with just using OR ? 仅使用OR什么问题?

SELECT * 
FROM status 
WHERE author IN (SELECT user1 FROM friends WHERE user2='$username' AND accepted='1') 
    OR author IN (SELECT user2 FROM friends WHERE user1='$username' AND accepted='1')
    OR author = '$username'

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

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