473,385 Members | 1,876 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,385 software developers and data experts.

Subquery in Join Statement

I have the following query I wrote in MySQL 5.0

Select
eq_employees.empid,
eq_sigreturns.returnid
From
eq_employees
Left Join eq_sigreturns ON eq_employees.empid
=
eq_sigreturns.empid
in (
Select eq_sigreturns.returnid,
eq_sigreturns.empid
From
eq_sigreturns
where
eq_sigreturns.`month` = '12' AND
eq_sigreturns.`year` = '2005'
)

To return all records from the employees table and the returnid from
the sigreturns table where the month and year match.

The query throws an error message, I assume meaning I can't have a
subquery in my join statement. However anywhere else I put the
subquery seems to negate the left join and I only get empids with
matching records in the sigreturns table

Thanks,
Kevin

Mar 1 '06 #1
2 1731
So I made the following change

Select
eq_employees.empid,
eq_sigreturns.returnid in
(
Select eq_sigreturns.returnid
From
eq_sigreturns
where
eq_sigreturns.`month` = '12' AND
eq_sigreturns.`year` = '2005'
)
From
eq_employees
Left Join eq_sigreturns ON eq_employees.empid = eq_sigreturns.empid

I get all records from the eq_emloyees database but I get a 0 instead
of the returnid for records that meet the query requirements.

Mar 1 '06 #2
"kevinjbowman" <ke**********@gmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
... From eq_employees
Left Join eq_sigreturns ON eq_employees.empid = eq_sigreturns.empid
in (
Select eq_sigreturns.returnid,
eq_sigreturns.empid ...


1. You can only do one predicate at a time. If you do an expression like
a = b IN (1,2,3)
The order of evaluation is unclear. Does it test b IN (1,2,3) and return
0 or 1, then compare that 0 or 1 to a? Or does it compare a = b and return
0 or 1, then compare that to (1,2,3)? Depends on the order of evaluation
between = and IN. But this expression definitely does not imply the logic
of a = b AND b IN (1,2,3).

2. When using IN (SELECT ...), put only one column in the select-list of the
subquery, the column which you compare to the expression on the
left-hand-side of the IN.

This could work:

SELECT ...
FROM eq_employees LEFT JOIN eq_sigreturns
ON eq_employees.empid = eq_sigreturns.empid
AND eq_sigreturns.empid IN ( SELECT empid FROM eq_sigreturns WHERE
`month` = 12 AND `year` = 2005)

In this case, this is equivalent to the following simplified form:

SELECT ...
FROM eq_employees LEFT JOIN eq_sigreturns
ON eq_employees.empid = eq_sigreturns.empid
AND eq_sigreturns.`month` = 12 AND eq_sigreturns.`year` = 2005

Regards,
Bill K.
Mar 1 '06 #3

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

Similar topics

6
by: Dan Evans | last post by:
Hi, Can anyone help me on a little problem I am having with some SQL - in particular on a subquery. I am setting up a database in Access for a voluntary group which runs
7
by: Kannan | last post by:
Hello, I have a situation which would essentially use a co-related subquery. I am trying to avoid using a co-related subquery due to its slow performanc and use a join statement instead. Here...
7
by: Andrew Mayo | last post by:
Here's a really weird one for any SQL Server gurus out there... We have observed (SQL Server 2000) scenarios where a stored procedure which (a) begins a transaction (b) inserts some rows into...
2
by: edself | last post by:
Greetings, I am semi-new to Access and have a query question. I presume the solution is easy, but need some help. I have created a database with a Contact table. The contact table contains...
8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
3
by: Dave | last post by:
I have a list of towns in tblTownData, and a list of people's names in tblNames. During a year, there are multiple events where more names are added in tblNames, for a particular town. ie:...
22
by: Kevin Murphy | last post by:
I'm using PG 7.4.3 on Mac OS X. I am disappointed with the performance of queries like 'select foo from bar where baz in (subquery)', or updates like 'update bar set foo = 2 where baz in...
0
by: kiran2nirvan | last post by:
hi please help in solving this i am recieving this error"Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as...
1
by: mipo1984 | last post by:
I have subquery into a principal query, and i need the subquery return me only the last row of all results, but i can`t use "order by <field> desc " in the subquery because this return me an error,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.