473,796 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Subquery in Join Statement

I have the following query I wrote in MySQL 5.0

Select
eq_employees.em pid,
eq_sigreturns.r eturnid
From
eq_employees
Left Join eq_sigreturns ON eq_employees.em pid
=
eq_sigreturns.e mpid
in (
Select eq_sigreturns.r eturnid,
eq_sigreturns.e mpid
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 1753
So I made the following change

Select
eq_employees.em pid,
eq_sigreturns.r eturnid in
(
Select eq_sigreturns.r eturnid
From
eq_sigreturns
where
eq_sigreturns.` month` = '12' AND
eq_sigreturns.` year` = '2005'
)
From
eq_employees
Left Join eq_sigreturns ON eq_employees.em pid = eq_sigreturns.e mpid

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
"kevinjbowm an" <ke**********@g mail.com> wrote in message
news:11******** **************@ v46g2000cwv.goo glegroups.com.. .
... From eq_employees
Left Join eq_sigreturns ON eq_employees.em pid = eq_sigreturns.e mpid
in (
Select eq_sigreturns.r eturnid,
eq_sigreturns.e mpid ...


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.em pid = eq_sigreturns.e mpid
AND eq_sigreturns.e mpid 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.em pid = eq_sigreturns.e mpid
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
1743
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
4004
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 is what I am trying to do: Tables: ======== Limit
7
9229
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 a table (c) re-queries another table using a subquery which references the inserted table (correlated or not)
2
3020
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 address fields among other things. Because some contacts share the same address, I included a boolean field, PrimaryContact. If true, a given contact's record contains the address info for that contact. If PrimaryContact is false, then another...
8
19603
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 couple of tables in my database using INNER JOINS and the WHERE clause to specify the required constraints. However, I also want to read two fields from a *single* record from a table called 'Locations' and then apply one of these field's values...
3
22433
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: TOWNID EVENTID NAME 1 1 John Doe 1 1 Jane Doe 1 2 Bell E. Dancer 1 2 Frank N. Furter
22
3363
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 (subquery)'. PG always seems to want to do a sequential scan of the bar table. I wish there were a way of telling PG, "use the index on baz in your plan, because I know that the subquery will return very few results". Where it really matters, I have...
0
2074
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 an expression." for the code CREATE procedure . (@frmMasterID int,
1
2081
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, nither "fetch first 1 rows only" because this return a error too. Any sugestion? this es the SQL... SELECT C.NOMBRE,V.COBJCOD,CS.DESC,V.CSUBCOD, N.NOMBRE,CS.NIVEL,V.SUBLIBRO, V.TSUBLIBRO,CS.CAT5COD,CAT7COD, ...
0
9536
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10468
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10021
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9063
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7559
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6802
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4131
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.