Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

mysql discount and count to be used in same query

Posted on 2012-03-24
2
484 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Creating and Managing Databases with phpMyAdmin in cPanel.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
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…

856 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