从mysql查询中遗漏了一些结果

时间:2012-01-08 18:37:57

标签: mysql sql if-statement

我有这么有趣的情况。 这是名为“mirror_data”=>的表

+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id_mir     | int(11)      | NO   | PRI | NULL    | auto_increment |
| local      | varchar(255) | YES  |     | NULL    |                |
| local_mir  | varchar(255) | YES  |     | NULL    |                |
| remote     | varchar(255) | YES  |     | NULL    |                |
| remote_mir | varchar(255) | YES  |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+

这是第二个名为“data”的表


+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| id              | int(11)      | NO   |     | 0       |       | 
| localParty      | varchar(255) | YES  |     | NULL    |       |
| other columns ...                                             |
+-----------------+--------------+------+-----+---------+-------+

我正在从数据表中检索信息,但我希望当(从“数据”表)列localParty等于(table from“mirror_data”)列本地时,然后从local_mir列检索信息(也来自“mirror_data”) “表”,否则当标记列彼此不相等时,从localParty检索信息。但我想从“数据”表中检索整个信息。

这里是mirror_data inserted values =>

+--------+--------+-----------+--------+------------+
| id_mir | local  | local_mir | remote | remote_mir |
+--------+--------+-----------+--------+------------+
|      1 | 715715 | Something | NULL   | NULL       |
|      2 | 1000   | some      | NULL   | NULL       |
+--------+--------+-----------+--------+------------+

这是我的代码我是如何“做”这个=>

select IF(o.localParty=m.local,m.local_mir,o.localParty) FROM data as o, mirror_data as m limit 0,10;

这里也是这个查询的结果(缺少一些值)=>

+---------------------------------------------------+
| IF(o.localParty=m.local,m.local_mir,o.localParty) |
+---------------------------------------------------+
| Something                                         |
| 715715                                            |
| 14                                                |
| 14                                                |
| Something                                         |
| 715715                                            |
| Something                                         |
| 715715                                            |
| 978080                                            |
| 943080                                            |
+---------------------------------------------------+

就像你看到的那样,在这种情况下,数字715715没有改变,因为在mirror_data表中有两个值(当它们会更多时,更有可能不接收预期的结果),我如何编写查询来实现我的目标?

只想从“数据”表中检索整个信息,但如果标记的情况发生,则更改(localparty)(local = localparty)

2 个答案:

答案 0 :(得分:1)

您实际上正在加入表datamirror_data,但在WHERE子句中没有连接条件来定义两个表的关联方式。

SELECT
  IF(o.localParty = m.local, m.local_mir, o.localParty) 
FROM
  data as o,
  mirror_data as m
/* Need an equivalent column to join on */
WHERE o.some_column = m.some_related_column
limit 0,10;

使用较新的首选JOIN语法:

SELECT
  IF(o.localParty = m.local, m.local_mir, o.localParty) 
FROM
  data AS o,
  JOIN mirror_data AS m ON o.some_column = m.some_related_column
LIMIT 0, 10;

答案 1 :(得分:1)

您应该使用LEFT JOIN,以便在data表格中包含所有信息,并在mirror_table时提供信息。

如果没有匹配,那么m.local_mir将为NULL,因此您可以使用IFNULL

编辑

以回答评论中的子问题

SELECT
   IFNULL(m.local_mir, o.localParty) as local,
   IFNULL(m.remote_mir, o.remoteParty) as remote
FROM 
   data as o
   LEFT JOIN mirror_data as m 
      ON (o.localParty = m.local OR o.remoteParty = m.remote)
LIMIT 
   0,10;