473,419 Members | 4,349 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,419 software developers and data experts.

Left Join Query Problem

Coldfire
289 100+
Two tables

Expand|Select|Wrap|Line Numbers
  1. customer (id,name) = {
  2. '1','ABC'; 
  3. '2','DEF';
  4. '3','GHI'
  5. }
Expand|Select|Wrap|Line Numbers
  1. images (id,customer_id,subscription_id,imageURL) = {
  2. '1','1','123','abc123.jpg'; 
  3. '2','2','456','asdjkl.png'; 
  4. '3','3','456','asad.gif'; 
  5. }
I want to get the customer.name, images.id, and images.imageURL from the two tables. But,

the subscription_id should be '456'

AND

Join on customer id.

The query I am using is,

SQL = "Select c.name,i.id,i.imageURL from customer c LEFT JOIN images i ON c.id=i.customer_id WHERE i.subscription_id='456'"

What I need is
Expand|Select|Wrap|Line Numbers
  1. (customer.name, images.id, imageURL) 
  2. {ABC,1,NULL},
  3. {DEF,2,'asdjkl.png'},
  4. {GHI,3,'asad.gif'}
But it is returnning the last 2 rows as the WHERE clause is filtering the first ROW. But, I need NULL for the first row.

It is because I want to get the images of all the customers when the subscription is '456' if the subscription is something else then NULL should be returned.

Should I use something else instead of LEFT JOIN or what ?

any help
Jun 4 '10 #1

✓ answered by Uncle Dickie

Use a CASE statement in the SELECT part rather than a WHERE condition.

Something like:
Expand|Select|Wrap|Line Numbers
  1. CASE subscription_id
  2.   WHEN 456 THEN imageURL
  3.   ELSE null
  4. END
Hope this helps!

2 1513
Use a CASE statement in the SELECT part rather than a WHERE condition.

Something like:
Expand|Select|Wrap|Line Numbers
  1. CASE subscription_id
  2.   WHEN 456 THEN imageURL
  3.   ELSE null
  4. END
Hope this helps!
Jun 7 '10 #2
ThatThatGuy
449 Expert 256MB
@Coldfire
Obviously, you will get the imageURL field as null as you've applied LEFT JOIN..

Instead you should use INNER JOIN which will yield even more better results
Jun 7 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Dave | last post by:
To build a grid, all the distinct rows from T1 are required, and only those from T2 which fall btn 2003-09-11 and 2003-09-18. In the following example: A is included because it is within the date...
0
by: Soefara | last post by:
Dear Sirs, I am experiencing strange results when trying to optimize a LEFT JOIN on 3 tables using MySQL. Given 3 tables A, B, C such as the following: create table A ( uniqueId int not...
0
by: Leif Wessman | last post by:
Hi! Please have a look at the following query: SELECT BOOKS.ISBN, BOOKS.TITLE, IMAGES.WEIGHT, IMAGES.WIDTH FROM BOOKS LEFT JOIN IMAGES ON BOOKS.ISBN = IMAGES.ID WHERE MATCH(BOOKS.TITLE)...
1
by: David Boone | last post by:
Please excuse the newbie questions, but I'm fairly new to Access though familiar with databases in general (experience with MySQL and PostgreSQL). The basic idea is that there is table of...
7
by: deko | last post by:
SELECT tblTxAcct.TxAcctName, tblTxType.TxTypeName, Nz(qryTxAcctListCt.TxCount, 0) AS TxCt FROM (tblTxAcct INNER JOIN tblTxType ON tblTxAcct.TxType_ID=tblTxType.TxType_ID) LEFT JOIN qryTxAcctListCt...
3
by: phillip.s.powell | last post by:
I have to produce a query to obtain a single record, however, the tables I work with have a very strange relationship: Table: student Fields: id first_name last_name email school_type_id...
0
by: mlarson | last post by:
I have a program that worked fine then they needed to be able to also see the empty cells (inmate cells) on a housing unit when they ran the query. So what I had to do was take two tables and...
1
by: sesmap | last post by:
I am not certain if I set this up correctly - all help would be appreciated: SELECT .ACCOUNTID, .PUBCODE, .NAME, .PIATYPE, .FOD, .SUBSLNGTH, .RATECODE, .CURRENTBAL, .EOGDATE, .TELEPHONE,...
2
by: imnewtoaccess | last post by:
Hello everybody, I have two tables : ac_online_scanned_tkts_1 and TK_IMPORT_TICKET1. Both have external barcodes of tickets, but not all of them in ac_online_scanned_tkts_1 table exist in...
2
by: knkk | last post by:
Please see this: WARNING Error while executing query: select event_type, EventId, VenueId, Price, Schedule, ev.startDate, ev.endDate, events.name eventname, movies.name moviename from...
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
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:
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
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...

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.