0

我有以下 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

回报:

sentTo 2 但未 sendFrom 1

已发送至 3 但未发送自 1

发送自 2 但未发送至 1

sentFrom 2 和 sentTo 3

4

2 回答 2

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)
于 2012-11-07T04:25:43.673 回答
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。在这种情况下,它将返回Cartesian product查询 1 到查询 2 的记录。

于 2012-11-07T04:32:12.767 回答