在第二个表中选择一行和一行

时间:2014-08-25 11:22:08

标签: mysql

我有2个表名为orders,第2名为rewinding

第一orders有一条记录

id=1, job_code=597, job_name=xyz  

第二个rewinding有一个或多个记录

id=5, job_code=597, weight=254,remarks=foo
id=6, job_code=600, weight=765,remarks=foo
id=7, job_code=597, weight=594,remarks=foo
id=8, job_code=597, weight=54,remarks=foo

现在,我想选择rows and columns所有code=597并且只记录orders where job_code=597

的{{1}}

2 个答案:

答案 0 :(得分:0)

如果orders中有多个行job_code并且您只想要一行,则在子查询中使用limit

SELECT *
FROM (SELECT o.* FROM orders o WHERE o.job_code = 597 LIMIT 1
     ) as o597 JOIN
     rewinding r
    ON o597.job_code = r.job_code;

答案 1 :(得分:-1)

这个怎么样?

SELECT *, orders.id orders_id, rewinding.id rewinding_id 
FROM orders
JOIN rewinding ON orders.job_code = rewinding.job_code
WHERE orders.job_code = 597

演示:http://sqlfiddle.com/#!2/b1ad6/4