Help with query

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
pepe_lepew1962
Forum Commoner
Posts: 44
Joined: Thu Nov 20, 2008 10:29 am

Help with query

Post by pepe_lepew1962 »

Hello:

I am trying to get a mysql query together an not quite sure how to apply it, whether it is with distinct, join and all. What I am looking is to get the ID, qty ( number of users ), max and min number where the ID's match. Can anyone help me with this please.


Name ID Name
123ABC Engine
649DFA Transmission
854UDF Window


User ID User Name User Amount
123ABC Paul 300
123ABC John 190
649DFA Ian 500
123ABC Mary 210
123ABC Mark 225
649DFA Steve 360




Required Result:

ID Qty Max Mix
123ABC 4 300 190
649DFA 2 500 360
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Help with query

Post by mikosiko »

Code: Select all

SELECT a.ID, count(b.ID), MAX(b.qty), MIN(b.qty)
FROM tbl1 AS a JOIN tbl2 AS b ON (a.id = b.id)
group by a.ID;
replace with you table/fieldnames accordingly
pepe_lepew1962
Forum Commoner
Posts: 44
Joined: Thu Nov 20, 2008 10:29 am

Re: Help with query

Post by pepe_lepew1962 »

Mikosiko you rock.
Thank You.
Post Reply