sql结果中的附加列

时间:2016-05-22 03:48:42

标签: mysql

给出以下MySQL语句:

SELECT name FROM jobtype_descriptions WHERE id IN (SELECT job_description FROM `table2` WHERE id IN (10684,10580,10583,10582,10581,10579,10576))

执行时,它会给我以下结果:

enter image description here

但是我希望将内部select语句中的ids job_description列包含为第一列。这是内部select语句的结果:

enter image description here

我尝试使用第二个select语句的别名,但没有用。

3 个答案:

答案 0 :(得分:1)

您需要按名称包含所需的列,即

SELECT id, name FROM jobtype_descriptions WHERE id IN (SELECT job_description FROM `table2` WHERE id IN (10684,10580,10583,10582,10581,10579,10576))

答案 1 :(得分:1)

将您的查询修改为

SELECT t2.id, jtd.name 
FROM jobtype_descriptions jtd
JOIN table2 t2 ON jtd.id = t2.job_description
WHERE t2.id IN (10684,10580,10583,10582,10581,10579,10576);

答案 2 :(得分:1)

尝试

SELECT name, id FROM jobtype_descriptions WHERE id IN (SELECT job_description FROM `table2` WHERE id IN (10684,10580,10583,10582,10581,10579,10576))

您通常可以通过按名称引用它们来在选择中包含所需的任何列。例如,SELECT col1, col2, col3 FROM ...