473,395 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Writing SQL query without subselect

I'm stuck on a host that is still running MYSQL version 3.

I need to flatten out a relationship to AND a set of criteria.

In a nutshell

User
UserID
UserEmail
UserOther

UserSkills
UserID
SkillCode

Any user may have zero, one, or many UserSkills.

I need to find a list of UserIDs for Users who have SkillCodes A and SkillCode
B.

How do I write the query (queries)?
Jul 17 '05 #1
11 2278
Steven Stern wrote:
I'm stuck on a host that is still running MYSQL version 3.
Well even if you had a host with 4.0.x you still wouldn't be able to do
subqueries. They won't be available until 4.1.x is released, and it's still
in beta (or is it gamma now?) Even then I suspect not many hosts will start
to host it until at least another couple of minor revisions have passed.
I need to flatten out a relationship to AND a set of criteria.

In a nutshell

User
UserID
UserEmail
UserOther

UserSkills
UserID
SkillCode

Any user may have zero, one, or many UserSkills.

I need to find a list of UserIDs for Users who have SkillCodes A and
SkillCode B.

How do I write the query (queries)?


You need to join the table to itself (which is often more efficient than
using a subquery anyway) along the lines of this:

SELECT UserID
FROM User u
INNER JOIN UserSkills us1 ON u.UserID = us1.UserID
INNER JOIN UserSkills us2 ON u.UserID = us2.UserID
WHERE us1.SkillCode = 'A'
AND us2.SkillCode = 'B'

I have *not* tested this but it should work.

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #2
>User
UserID
UserEmail
UserOther

UserSkills
UserID
SkillCode

Any user may have zero, one, or many UserSkills.

I need to find a list of UserIDs for Users who have SkillCodes A and SkillCode
B.

How do I write the query (queries)?


Untested, but this should work.

SELECT u.UserID
FROM User u, UserSkills a, UserSkills b
WHERE u.UserID = a.UserID and a.UserID = b.UserID and
a.SkillCode = 'A' and b.SkillCode = 'B';

This gets much messier when the number of skill codes is variable -
you probably end up having PHP construct a query in a loop.

Gordon L. Burditt

Jul 17 '05 #3
Gordon Burditt wrote:
User
UserID
UserEmail
UserOther

UserSkills
UserID
SkillCode

Any user may have zero, one, or many UserSkills.

I need to find a list of UserIDs for Users who have SkillCodes A and
SkillCode B.

How do I write the query (queries)?


Untested, but this should work.

SELECT u.UserID
FROM User u, UserSkills a, UserSkills b
WHERE u.UserID = a.UserID and a.UserID = b.UserID and
a.SkillCode = 'A' and b.SkillCode = 'B';

This gets much messier when the number of skill codes is variable -
you probably end up having PHP construct a query in a loop.


This is essentially the same as the inner join query I posted. The only
difference is I find the inner join type queries much easier to read
because it's more clear where the relationships between the tables are.

I used to always write my queries the way that Gordon has here but then
started working for about a year at a Microsoft shop using SQL Server, and
they had some great standards documents and always wrote their queries
using inner joins. I found it a little odd to start with but then quickly
adopted the style for all my database work because it makes more complex
queries much easier to read.

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #4
go***********@burditt.org (Gordon Burditt) wrote:

[snip]
This gets much messier when the number of skill codes is variable -
you probably end up having PHP construct a query in a loop.


select userid from userskills
group by userid
where skillcode in ('A','B','C','D')
having count(*)=4

The in has the list and the count is compared to the number of
skills.

If you want more user data,

select <whatever> from user
where userid in (<above query>)
order by <whatever>

Sincerely,

Gene Wirchenko

Computerese Irregular Verb Conjugation:
I have preferences.
You have biases.
He/She has prejudices.
Jul 17 '05 #5
Gene Wirchenko wrote:
This gets much messier when the number of skill codes is variable -
you probably end up having PHP construct a query in a loop.


select userid from userskills
group by userid
where skillcode in ('A','B','C','D')
having count(*)=4


Nice solution :)

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #6
Chris Hope <bl*******@electrictoolbox.com> wrote:

I used to always write my queries the way that Gordon has here but then
started working for about a year at a Microsoft shop using SQL Server, and
they had some great standards documents and always wrote their queries
using inner joins. I found it a little odd to start with but then quickly
adopted the style for all my database work because it makes more complex
queries much easier to read.


Do you think so? Maybe you could post a good example that shows this.

For me, most SQL reads like an English sentence. The INNER JOIN syntax
disturbs that. Compare this:

SELECT r.RID, r.LastName, p.FirstName, r.Address, r.City, r.State,
u.CollegeName
FROM
((registration r INNER JOIN person p ON r.RID = p.RID)
INNER JOIN university u ON p.CollegeID=u.CollegeID)
WHERE
r.LastName = 'Smith';

to this:

SELECT r.RID, r.LastName, p.FirstName, r.Address, r.City, r.State,
u.CollegeName
FROM
registration r,
person p,
university r
WHERE r.RID = p.RID
AND p.CollegeID = u.CollegeID
AND r.LastName = 'Smith';

For me, the second example reads more naturally. It gathers the list of
affected tables into one easily identifiable place, and makes the
connections part of the WHERE selection process. Query optimizers will
make these exactly equivalent. It gets even worse as the number of tables
increases.

It's probably a personal preference thing.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 17 '05 #7
Tim Roberts wrote:
Chris Hope <bl*******@electrictoolbox.com> wrote:

I used to always write my queries the way that Gordon has here but then
started working for about a year at a Microsoft shop using SQL Server, and
they had some great standards documents and always wrote their queries
using inner joins. I found it a little odd to start with but then quickly
adopted the style for all my database work because it makes more complex
queries much easier to read.


Do you think so? Maybe you could post a good example that shows this.

For me, most SQL reads like an English sentence. The INNER JOIN syntax
disturbs that. Compare this:

SELECT r.RID, r.LastName, p.FirstName, r.Address, r.City, r.State,
u.CollegeName
FROM
((registration r INNER JOIN person p ON r.RID = p.RID)
INNER JOIN university u ON p.CollegeID=u.CollegeID)
WHERE
r.LastName = 'Smith';

to this:

SELECT r.RID, r.LastName, p.FirstName, r.Address, r.City, r.State,
u.CollegeName
FROM
registration r,
person p,
university r
WHERE r.RID = p.RID
AND p.CollegeID = u.CollegeID
AND r.LastName = 'Smith';

For me, the second example reads more naturally. It gathers the list of
affected tables into one easily identifiable place, and makes the
connections part of the WHERE selection process. Query optimizers will
make these exactly equivalent. It gets even worse as the number of tables
increases.

It's probably a personal preference thing.


I would agree with it being a personal preference because when the database
server optimises your query it's not going to make a difference either way,
as they are fundamentally the same query.

To me the inner join syntax is much more readable because to me it's more
easy to see what you are joining them, than having to work it out from
looking at the where clause.

You don't actually need those braces around the from and inner join bits in
your example, and I'd reformat it like so (which, to me, makes it a lot
more readable):

SELECT r.RID, r.LastName, p.FirstName, r.Address, r.City, r.State,
u.CollegeName
FROM registration r
INNER JOIN person p ON r.RID = p.RID
INNER JOIN university u ON p.CollegeID=u.CollegeID
WHERE r.LastName = 'Smith';

I cannot give you a better example than this as it simply reads better to
me, whereas the non inner join syntax reads better to you :)

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #8
I noticed that Message-ID: <mv********************************@4ax.com>
from Tim Roberts contained the following:
For me, the second example reads more naturally.


I'm with you there.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #9
On Thu, 14 Oct 2004 14:49:03 +1300, Chris Hope
<bl*******@electrictoolbox.com> wrote:

[Aargh! The amended fu got me. Reposting]
Gene Wirchenko wrote:
This gets much messier when the number of skill codes is variable -
you probably end up having PHP construct a query in a loop.


select userid from userskills
group by userid
where skillcode in ('A','B','C','D')
having count(*)=4


Nice solution :)


Minor syntax quibble: does MySQL allow "where" after "group by"?

Lemming
--
Curiosity *may* have killed Schrodinger's cat.
Jul 17 '05 #10
Lemming wrote:
On Thu, 14 Oct 2004 14:49:03 +1300, Chris Hope
<bl*******@electrictoolbox.com> wrote:

[Aargh! The amended fu got me. Reposting]
Gene Wirchenko wrote:
This gets much messier when the number of skill codes is variable -
you probably end up having PHP construct a query in a loop.

select userid from userskills
group by userid
where skillcode in ('A','B','C','D')
having count(*)=4


Nice solution :)


Minor syntax quibble: does MySQL allow "where" after "group by"?


Looks like he got the query in the wrong order, because you are correct,
group by must be after the where clause. Therefore his query should be:

select userid from userskills
where skillcode in ('A','B','C','D')
group by userid
having count(*)=4

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #11
On Fri, 15 Oct 2004 14:07:51 +1300, Chris Hope
<bl*******@electrictoolbox.com> wrote:
Looks like ...

[snip]

You keep changing the followup-to line and removing most of the
groups. Please don't do that. I don't read comp.lang.php, so if I
was to not change followup-to back to include the full list of groups
I would be posting to groups I don't read, so wouldn't see either my
own post nor any replies.

Lemming
--
Curiosity *may* have killed Schrodinger's cat.
Jul 17 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Theodor Ramisch | last post by:
Hi there, I've got a simple table with an ID and a parentID field which build the relation between the entries. Now I want to select some entries and count their childs. This is what I tried:...
3
by: David | last post by:
Consider this SQL Query: ----------------------------------------------------------------- SELECT c.CASE_NBR, DATEDIFF(d, c.CREATE_DT, GETDATE()) AS Age, c.AFFD_RCVD, c.PRV_CRD_ISS, x.RegE,...
4
by: Wanny | last post by:
Hi There, I can't seem to see what's wrong with the query below DELETE FROM Users_Details UD1 WHERE UD1.UserID = ( SELECT TOP 1 UD2.UserID FROM Users_Details UD2 WHERE UD1.useremail =...
3
by: krystoffff | last post by:
Hi I would like to paginate the results of a query on several pages. So I use a query with a limit X offset Y to display X results on a page, ok. But for the first page, I need to run the...
5
by: Todd | last post by:
Data related to the query I'm working on is structured such that TableA and TableB are 1-many(optional). If an item on TableA has children on TableB, I need to use the Max(tstamp) from Table B in...
7
by: Patrick Fisher | last post by:
Hi I have a table which Contains entries with RefCode field containing INVP or INVPD Common fields in each entry would be InvoiceNo, Total and PurTyp for example. You could have ...
4
by: ungahz | last post by:
hi all. im having a problem and wondering if anyone an help. i have the following tables along with column names (too many columns so im just including the relevent ones): folders ---------...
4
by: Richard | last post by:
In a manufacturing document control situation, a procedure revision table named has principal columns and and satellite data columns , , . A unique index and to ensure each procedure name has...
0
by: eltontodd | last post by:
I have a query that I need to run on a database that is on a SQL Server 7 installation. When I run the query on that database it takes forever. If I take the same query and run it on a database...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.