?
Solved

help with my sql query select in a select

Posted on 2012-03-18
3
Medium Priority
?
238 Views
Last Modified: 2012-03-18
Select UserID, (select count(*) from names where userid = userid) as totalcount from users where firstname like '%john%'


The error is "where userid = userid".  I don't know how to word this.  UserID's are unique accross both tables.  I want to return results for all firstnames like "%john%" out of my Users table, and also, for each record, I want the count(*) from names for that same UserID.
0
Comment
Question by:arthurh88
[X]
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
3 Comments
 
LVL 7

Accepted Solution

by:
micropc1 earned 2000 total points
ID: 37736229
You need an alias for the outer table...

Select u.UserID, (select count(*) from names where userid = u.userid) as totalcount 
from users u
where u.firstname like '%john%'

Open in new window

0
 
LVL 39

Expert Comment

by:appari
ID: 37736267
or try like this

Select users.UserID, count(names.userid) as totalcount
from users left outer join names
on users.userid = names.userid
where firstname like '%john%'
Group by users.UserID
0
 

Author Closing Comment

by:arthurh88
ID: 37736283
that was it!  I knew it was something easy
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

777 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