我有两张桌子
card_index
+-----+-----+-----+-----+
| uid | id1 | id2 | id3 |
+-----+-----+-----+-----+
| 1 | 125 | 129 | 145 |
| 2 | 127 | 128 | 132 |
+-----+-----+-----+-----+
和card_data
+----+------------+----------+...
| id | last_use | active |
+----+------------+----------+...
| 125| 10-07-2014 | true |
| 127| 27-02-2014 | true |
| 128| 15-06-2014 | true |
| 129| 10-01-2013 | false |
+----+------------+----------+... and so on
我想从'card_index'中选择'card_data'中'uid'的所有数据,因此'uid'= 1的结果应如下所示:
+----+------------+----------+
| id | last_use | active |
+----+------------+----------+
| 125| 10-07-2014 | true |
| 129| 10-01-2013 | false |
| 145| 13-01-2013 | true |
+----+------------+----------+
我做的SQL查询是:
SELECT * FROM card_data WHERE id IN (SELECT CONCAT(id1,',',id2,',',id3) FROM card_index WHERE uid = 1);
然而,它只选择第一行:
+----+------------+----------+
| id | last_use | active |
+----+------------+----------+
| 125| 10-07-2014 | true |
+----+------------+----------+
任何人都可以帮助我吗?我不知道出了什么问题。 我正在使用从FreeBSD上的源代码编译的mysql-server 5.5.38-log
答案 0 :(得分:0)
使用INNER JOIN
代替CONCAT
和WHERE id IN
的组合为我工作。
这是查询:
SELECT * FROM card_data
INNER JOIN card_index ON id1=id OR id2=id OR id3=id
WHERE uid = 1;
期望的结果:
+----+------------+----------+
| id | last_use | active |
+----+------------+----------+
| 125| 10-07-2014 | true |
| 129| 10-01-2013 | false |
| 145| 13-01-2013 | true |
+----+------------+----------+