MySQL Select Query Question

hoihtah

Diamond Member
Jan 12, 2001
5,183
0
76
ok, so i'm trying to query from a mysql database tables.
any help would be greatly appreciated.

table : buyer
-------------------------------
buyer_id | name
------------------------------
1 | john
2 | sally


table : fruit
------------------------------
fruit_id | fruit
------------------------------
1 | apple
2 | orange


table : purchase
-----------------------------
purchase_id | buyer_id | fruit_id
1 | 1 | 1
2 | 1 | 1
3 | 1 | 2
3 | 2 | 1

i'm trying to have one query that'll output something like this

name | fruit
john | 2x apple, 1x orange
sally | 1x orange

i'd normally use two queries and use php to script it...
but there's got to be a way to do this within mysql with one query.

anyone?
 

hoihtah

Diamond Member
Jan 12, 2001
5,183
0
76
Originally posted by: LoKe
If you're selecting from two different tables, I believe you'd have to use two seperate queries.

you can draw queries from two different tables... or three for this matter

SELECT purchase_id, buyer.name, fruit.fruit
FROM buyer, fruit, purchase
WHERE buyer.buyer_id=purchase.buyer_id AND fruit.fruit_id=purchase.fruit_id

if i do the query above it will list four lines

1, john, apple
2, john, apple
3, john, orange
4, sally, apple

i am wondering how i an merge two lines (1&2) into one.

how do i set up the query so that if same buyer_id appears multiple times, merge and concatenate into one line?
 

mooncancook

Platinum Member
May 28, 2003
2,874
50
91
this is the closest I can think of (in MS SQL syntax)

SELECT buyer.name, fruit.fruit, COUNT(purchase.buyerID) as PurchaseNo
FROM purchase JOIN buyer on buyers.buyer_ID=fruit.buyer_ID
JOIN fruit ON fruit.fruit_ID=purchase.fruit_ID
GROUP BY buyer.name, fruit.fruit
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: LoKe
If you're selecting from two different tables, I believe you'd have to use two seperate queries.

Don't give any more SQL advice

I might be able to give you a working query, but I'm not going to bother setting upthe whole database to do it.

Give me access to your database and I'll play with it.
 
Jun 4, 2005
19,723
1
0
Originally posted by: notfred
Originally posted by: LoKe
If you're selecting from two different tables, I believe you'd have to use two seperate queries.

Don't give any more SQL advice

I might be able to give you a working query, but I'm not going to bother setting upthe whole database to do it.

Give me access to your database and I'll play with it.

SQL is not one of my strong points.
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
SELECT buyer.name AS name, fruit.fruit AS fruit, COUNT(fruit.fruit_id) AS purchase
FROM purchase
LEFT JOIN buyer USING (buyer_id)
LEFT JOIN fruit USING (fruit_id)
GROUP BY name, fruit


Output:
name fruit purchase
john apple 2
john orange 1
sally apple 1
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
SELECT * FROM purchase
LEFT JOIN fruit on fruit.fruit_id = purchase.fruid_id
LEFT JOIN buyer on buyer.buyer_id = purchase.buyer_id
 

hoihtah

Diamond Member
Jan 12, 2001
5,183
0
76
i tried to follow the link that you gave...
but it's a bit confusing.

oracle syntex is a little bit different from mysql.

thanks a bunch though, for pointing me in the right direction.
i'm still digging... to see how i can get this set up.
 

Thyme

Platinum Member
Nov 30, 2000
2,330
0
0
It's fairly complicated to do in SQL, it seems. You might want to do as suggeseted to get a 1NF table and use script from there.
 

hoihtah

Diamond Member
Jan 12, 2001
5,183
0
76
so far, i've gotten this far.

SELECT purchase_id, buyer.name,
(SELECT GROUP_CONCAT(DISTINCT fruit.fruit ORDER BY fruit.fruit SEPARATOR ', ')
AS fruit
FROM buyer, fruit, purchase
WHERE buyer.buyer_id=purchase.buyer_id AND fruit.fruit_id=purchase.fruit_id

now, i'm looking for ways to concat the quantity in.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |