我有以下2种SQL语法,我希望将它们合并为1种SQL语法
SELECT sentFrom
FROM tableName
WHERE id = :varid
AND sentFrom != :vartext
SELECT sentTo
FROM tableName
WHERE id = :varid
AND sentTo != :vartext
我想我应该举个例子:
MYSQL表
-----------------------------------
sentFrom sentTo
1 2
1 3
2 1
2 3
查询
select sentFrom where sentFrom != 1 and select sentTo where sentTo != 1
返回:
已发送2但未发送从1
已发送至3但未从1发送
sentFrom 2但未发送到1
sentFrom 2和sentTo 3
答案 0 :(得分:0)
Syntex of select query is :
Select Colname1,Colname2,.... from tablename
where colname=value and/or ...
group by cond..
having cond...
order by colname, colname asc/desc
======================================
Ex:
SELECT sentFrom,sentTo FROM tableName
WHERE id = :varid AND (sentFrom != :vartext or sentTo != :vartext)
答案 1 :(得分:0)
我认为这应该有所帮助。
SELECT a.sentFrom , b.sentTo FROM
(SELECT id, sentFrom FROM tableName WHERE id = :varid AND sentFrom != :vartext) a
JOIN
(SELECT id, sentTo FROM tableName WHERE id = :varid AND sentTo != :vartext) b
ON a.id = b.id;
如果您认为ID
在每个查询中选择的行中不常见,请删除连接条件ON a.id = b.id
。在这种情况下,它将返回查询1的Cartesian product
个记录来查询2.