MYSQL选择语句条件

时间:2010-07-07 21:30:19

标签: sql mysql query-optimization

查询:

SELECT u.deviceID, u.userName, u.contactNo, u.rating
  FROM User u
 INNER JOIN TaxiQuery t ON u.deviceID = t.seat1
                        OR u.deviceID = t.seat2
                        OR u.deviceID = t.seat3
                        OR u.deviceID = t.seat4
 WHERE t.queryID = 3;

+--------------------------------------+----------+-----------+--------+
| deviceID                             | userName | contactNo | rating |
+--------------------------------------+----------+-----------+--------+
| 00000000-0000-1000-8000-0016CB8B3C8E | uuuuuu   |     55555 |      5 |
+--------------------------------------+----------+-----------+--------+

描述用户;

+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| deviceID  | varchar(100) | NO   | PRI | NULL    |       |
| userName  | varchar(100) | YES  |     | NULL    |       |
| contactNo | int(11)      | YES  |     | NULL    |       |
| emailAddr | varchar(100) | YES  |     | NULL    |       |
| rating    | int(11)      | YES  |     | NULL    |       |
+-----------+--------------+------+-----+---------+-------+

的MySQL>描述taxiQuery;

+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| queryID      | int(11)      | NO   | PRI | NULL    | auto_increment |
| destination  | varchar(100) | YES  |     | NULL    |                |
| deptTime     | varchar(100) | YES  |     | NULL    |                |
| startingPt   | varchar(100) | YES  |     | NULL    |                |
| boardingPass | varchar(100) | YES  |     | NULL    |                |
| miscInfo     | varchar(100) | YES  |     | NULL    |                |
| seat1        | varchar(100) | YES  |     | NULL    |                |
| seat2        | varchar(100) | YES  |     | NULL    |                |
| seat3        | varchar(100) | YES  |     | NULL    |                |
| seat4        | varchar(100) | YES  |     | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+

我想要的是显示用户的信息,如果它们存在于TaxiQuery中(seat1 / seat2 / seat3 / seat4)。但是当我们假设它们是3时,我只能输出一个结果。

当seat1,seat2,seat3,seat4包含用户的deviceID时,我是否知道如何修改mysql语句以显示用户信息(seat1-4是User表的deviceID的外键)? p>

1 个答案:

答案 0 :(得分:0)

据我所知,如果你不进行INNER连接,它应该可以工作。我认为INNER关键字告诉mySQL只包含每个源最多一次,因此当你实际需要最多四个(每个座位一个)时,它只会使用TaxiQuery的一个副本。