2013-12-10 51 views
-2

我有一個表命名爲batch與MySQL選擇查詢時遇到

batch_id  unit_size  product_id 
2    100000   c001 
3    30000   t008 

命名爲batch_process

batch_id  process_id  status 
2    100000   0 
2    100000   1 
2    100000   2 
2    100000   1 
3    30000   2 
3    30000   2 
3    30000   2 
3    30000   2 

現在我想輸出3 AS batch_id形式batch表,因爲在batch_process表中的所有其他表process_id狀態爲2batch_id = 3

如何選擇它。

+1

不清楚!你能展示一個樣本輸出嗎? – Damodaran

+3

就像這樣:http://sqlfiddle.com/#!2/aecc39/2? – Passerby

+0

@Passerby它的工作原理。謝謝你的幫助 –

回答

0

這種查詢可以幫助你

SELECT batch1.batch_id,batch_process1.process_id 
FROM batch_process AS batch_process1, 
batch AS batch1 
WHERE batch1.batch_id = batch_process1.batch_id 
AND batch_process1.status=2 
0

如果妳只是想選擇BATCH_ID狀態爲2,那麼你可以這樣寫:

select distinct batch_id from batch_process where status=2 

但如果u想查詢從批表信息不只是batch_id,你可以這樣寫:

SELECT batch.batch_id, 
     batch.unit_size, 
     batch.product_id 
LEFT JOIN batch_process ON batch.batch_id=batch_process.batch_id 
WHERE batch_process.status=2