比较两个表并添加一个带有重复ID的临时列

时间:2016-08-30 13:05:51

标签: mysql

我有两张桌子

表1

+----+--------+----------+
| id | userId | assetId  | 
+----+--------+----------+
|  1 |    561 | 2256     |
|  2 |    561 | 2257     |
|  3 |    561 | 2258     |
|  4 |    561 | 2259     |
|  5 |    561 | 2265     |
+----+--------+----------+

表2

+---------+----------+
| assetId | assetName |
+---------+-----------+
|   2256  | Asset 1   |
|   2257  | Asset 2   |
|   2258  | Asset 3   |
|   2259  | Asset 4   |
|   2265  | Asset 5   |
|   2266  | Asset 6   |
|   2267  | Asset 7   |
|   2268  | Asset 8   |
|   2269  | Asset 9   |
|   2270  | Asset 10  |
+---------+-----------+

现在我想要一个带有临时列的结果("匹配"),其中列" assetId" s被比较。

+---------+----------+---------+
| assetId | assetName| matching |
+---------+----------+----------+
|   2256  | Asset 1  |  true    |
|   2257  | Asset 2  |  true    |
|   2258  | Asset 3  |  true    |
|   2259  | Asset 4  |  true    |
|   2265  | Asset 5  |  true    |
|   2266  | Asset 6  |  false   |
|   2267  | Asset 7  |  false   |
|   2268  | Asset 8  |  false   |
|   2269  | Asset 9  |  false   |
|   2270  | Asset 10 |  false   |
+---------+----------+----------+

这只能在一个查询中实现吗?

4 个答案:

答案 0 :(得分:0)

如果我理解你需要

SELECT assets."assetId", assets."assetName", ("userId" is not null) as matching FROM assets LEFT JOIN user_assets on assets."assetId" = user_assets."assetId";

假设,您提到的那个"表1和#34;是名称' user_assets'和"表2"命名为资产'。只需使用你的名字。

答案 1 :(得分:0)

这样的事情:

SELECT a.assetId,a.assetName, if(a.assetId=b.assetId,'true','false') as matching FROM `table2` as a
left join table1 as a on a.assetId=b.assetId

答案 2 :(得分:0)

您可以使用LEFT JOIN和IF,如下所示:

select table2.assetId, table2.assetName, IF(table2.assetId = table1.assetId, 'true', 'false') as matching from table2 LEFT JOIN table1 ON (table2.assetId = table1.assetId);

答案 3 :(得分:0)

DROP TABLE IF EXISTS user_asset;

CREATE TABLE user_asset
(userId INT NOT NULL
,assetId INT NOT NULL
,PRIMARY KEY(userid,assetid)
);

INSERT INTO user_asset VALUES
(561,2256),
(561,2257),
(561,2258),
(561,2259),
(561,2265),
(562,2265),
(562,2265),
(561,3000),
(562,2270);


DROP TABLE IF EXISTS asset;

CREATE TABLE asset
(assetId INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,assetName VARCHAR(20) NOT NULL UNIQUE
);

INSERT INTO asset VALUES
(2256,'Asset 1'),
(2257,'Asset 2'),
(2258,'Asset 3'),
(2259,'Asset 4'),
(2265,'Asset 5'),
(2266,'Asset 6'),
(2267,'Asset 7'),
(2268,'Asset 8'),
(2269,'Asset 9'),
(2270,'Asset 10');

SELECT a.*
     , COALESCE(ua.assetid = a.assetid,0) `matching` 
  FROM asset a 
  LEFT 
  JOIN user_asset ua 
    ON ua.assetid = a.assetid 
   AND ua.userid = 561;
+---------+-----------+----------+
| assetId | assetName | matching |
+---------+-----------+----------+
|    2256 | Asset 1   |        1 |
|    2257 | Asset 2   |        1 |
|    2258 | Asset 3   |        1 |
|    2259 | Asset 4   |        1 |
|    2265 | Asset 5   |        1 |
|    2266 | Asset 6   |        0 |
|    2267 | Asset 7   |        0 |
|    2268 | Asset 8   |        0 |
|    2269 | Asset 9   |        0 |
|    2270 | Asset 10  |        0 |
+---------+-----------+----------+
10 rows in set (0.00 sec)