2010-05-16 167 views
0

我有這些列的MySQL表:如何做這個查詢?

  • ID(自動遞增)
  • ID_BOOK(INT)
  • 價格(雙)
  • DATA(日期)

我知道兩個ID_BOOK值,例如,1和2

QUERY:

我必須提取所有PRICE(的ID_BOOK = 1和ID_BOOK = 2),其中DATA是一樣的!

表例如:

1 1 10.00 2010-05-16 

2 1 11.00 2010-05-15 

3 1 12.00 2010-05-14 

4 2 18.00 2010-05-16 

5 2 11.50 2010-05-15 

結果例子:

1 1 10.00 2010-05-16 

4 2 18.00 2010-05-16 

2 1 11.00 2010-05-15 

5 2 11.50 2010-05-15 

ID_BOOK = 2具有不2010-05-14,所以我跳它。

非常感謝!

+1

請張貼你的代碼迄今爲止已經寫入。人們通常不喜歡只爲你寫代碼。 – 2010-05-16 11:09:03

+0

'DATA'是故意的嗎?這是一個日期類型。它應該根據它的代表來命名,例如'PURCHASE_DATE'。 – 2010-05-16 11:10:08

回答

2

我已經猜出了你想要的結果,你需要用你的表的實際名稱替換myTable

SELECT * 
FROM myTable 
WHERE DATA IN 
(
    SELECT 
     DATA 
    FROM myTable 
    GROUP BY 
     DATA 
    HAVING COUNT(*) > 1 
) 
ORDER BY 
    DATA DESC, 
    ID_BOOK, 
    ID 
0
select PURCHASE_DATE, PRICE from table group by PURCHASE_DATE 
+0

您需要將'PRICE'包裝到一個聚合函數中才能使用。它還會返回日期爲'2010-05-14'的行,Damiano想要跳過該行,因爲該日期只有一行。 – 2010-05-16 11:22:48

0

這會給你的結果略有不同的格式,但它可能是有用的:

Select 
t1.id as t1_id, t1.id_book as t1_id_book, t1.price as t1_price, t1.data as t1_data, 
t2.id as t2_id, t2.id_book as t2_id_book, t2.price as t2_price, t2.data as t2_data 
from table t1 
inner join table t2 on t1.data = t2.data 
and t1.id_book = 1 
and t2.id_book = 2 

你會得到:

1 1 10.00 2010-05-16 4 2 18.00 2010-05-16 
2 1 11.00 2010-05-15 5 2 11.50 2010-05-15