怎么做这个查询?

时间:2010-05-16 11:06:10

标签: sql select mysql

我有一个包含以下列的mysql表:

  • ID (自动递增)
  • ID_BOOK (int)
  • 价格(双倍)
  • 数据(日期)

我知道两个 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所以我跳了它。

非常感谢你!

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

select PURCHASE_DATE, PRICE from table group by PURCHASE_DATE

答案 2 :(得分: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