Solved

mysql discount and count to be used in same query

Posted on 2012-03-24
2
474 Views
Last Modified: 2012-04-04
i have this query

select distinct(origin) from names order by id desc

now, i want to apply the count also to this query to actually count the pictures in the query

the pictures table is different and origin is the field which matches in names table and the pictures table

so how can i do it

any idea
0
Comment
2 Comments
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37760769
select COUNT(*), distinct(origin) from names order by id desc
0
 
LVL 24

Accepted Solution

by:
johanntagle earned 500 total points
ID: 37761947
select origin, count(id) from names group by origin;

Note that you cannot really effectively order by a column outside origin and count(id) because you already aggregated rows here.  Now if you want to join it with data with you pictures table you need something like:

select p.column1, count(n.id) from pictures p join names n on p.origin=n.origin group by p.column1

if you need to add more columns, just make sure you add them to both the select and group by clauses.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQl Server 2012 r2 - Why temp table shrinks from large query 4 35
Select only the top record in a left join 13 35
sql server computed columns 11 31
TSQL query to generate xml 4 33
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question