How to join nearly identical several queries into one?
Posted
by Devyn
on Stack Overflow
See other posts from Stack Overflow
or by Devyn
Published on 2010-05-12T19:59:39Z
Indexed on
2010/05/12
20:14 UTC
Read the original article
Hit count: 256
Hi, Assume I have an order_dummy
table where order_dummy_id, order_id, user_id, book_id, author_id
are stored. You may complain the logic of my table but I somehow need to do it that way. I want to execute following queries.
SELECT *
FROM order_dummy
WHERE order_id = 1
AND user_id = 1
AND book_id = 1
ORDER BY `order_dummy_id` DESC
LIMIT 1
SELECT *
FROM order_dummy
WHERE order_id = 1
AND user_id = 1
AND book_id = 2
ORDER BY `order_dummy_id` DESC
LIMIT 1
SELECT *
FROM order_dummy
WHERE order_id = 1
AND user_id = 1
AND book_id = 3
ORDER BY `order_dummy_id` DESC
LIMIT 1
Please keep in mind that several numbers of same book is included in one order. Therefore, I list order_dummy_id
by descending and limit 1
so only LATEST ORDER of A BOOK is shown. But my goal is to show other books in that way in one table. I used group by
like this ...
SELECT *
FROM order_dummy
WHERE order_id = 1
AND user_id = 1
GROUP BY book_id
but it only shows order_dummy_id
with ascending result. I have no idea anymore. Looking forward your kindness help!
© Stack Overflow or respective owner