简体   繁体   English

MySQL选择多列合一

[英]MySQL select multiple columns into one

I have two tables 我有两张桌子

card_index 卡片索引

+-----+-----+-----+-----+
| uid | id1 | id2 | id3 |
+-----+-----+-----+-----+
|  1  | 125 | 129 | 145 |
|  2  | 127 | 128 | 132 |
+-----+-----+-----+-----+

and card_data 和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

I want to select all data from 'card_data' by 'uid' from 'card_index', so the result for 'uid' = 1 should look like this: 我想从'card_index'中选择'uid'从'card_data'中选择所有数据,因此'uid'= 1的结果应如下所示:

+----+------------+----------+
| id |  last_use  |  active  |
+----+------------+----------+
| 125| 10-07-2014 |   true   |
| 129| 10-01-2013 |   false  |
| 145| 13-01-2013 |   true   |
+----+------------+----------+

The SQL query I made is: 我进行的SQL查询是:

SELECT * FROM card_data WHERE id IN (SELECT CONCAT(id1,',',id2,',',id3) FROM card_index WHERE uid = 1);

However it only selects firts row: 但是,它仅选择firts行:

+----+------------+----------+
| id |  last_use  |  active  |
+----+------------+----------+
| 125| 10-07-2014 |   true   |
+----+------------+----------+

Can anyone help me? 谁能帮我? I don't know what's wrong. 我不知道怎么了 I'm using mysql-server 5.5.38-log, compiled from source on FreeBSD 我正在使用从FreeBSD上的源代码编译的mysql-server 5.5.38-log

Using INNER JOIN instead of combination of CONCAT and WHERE id IN worked for me. 使用INNER JOIN代替CONCATWHERE id IN的组合为我工作。 Here's the query: 这是查询:

SELECT * FROM card_data
INNER JOIN card_index ON id1=id OR id2=id OR id3=id
WHERE uid = 1;

And the desired result: 和预期的结果:

+----+------------+----------+
| id |  last_use  |  active  |
+----+------------+----------+
| 125| 10-07-2014 |   true   |
| 129| 10-01-2013 |   false  |
| 145| 13-01-2013 |   true   |
+----+------------+----------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM