I'm trying to join data from two tables into one where the USSN IS NOT NULL;
It works perfectly without the "WHERE CONTACT2.USSN IS NOT NULL" added to it. I've tried different permutations without luck.
SELECT B.CONTACT,
B.LASTNAME,
B.PHONE1,
B.PHONE2,
B.PHONE3,
A.ACCOUNTNO,
A.USCOREEFX,
A.USCORETUC,
A.USCOREXPN,
A.USCORE1EFX,
A.USCORE1TUC,
A.USCORE1XPN
FROM CONTACT2 A
INNER JOIN CONTACT1 B
ON A.ACCOUNTNO = B.ACCOUNTNO
WHERE USSN IS NOT NULL
I figured it out.
SELECT B.CONTACT,
B.LASTNAME,
B.PHONE1,
B.PHONE2,
B.PHONE3,
A.ACCOUNTNO,
A.USCOREEFX,
A.USCORETUC,
A.USCOREXPN,
A.USCORE1EFX,
A.USCORE1TUC,
A.USCORE1XPN
FROM CONTACT2 A
INNER JOIN CONTACT1 B
ON A.ACCOUNTNO = B.ACCOUNTNO
WHERE A.USSN IS NOT NULL
Related
I have 3 table (property, facility, property_facility)
Now I want to get all facilities from facility table alone with property data to given property ID.
I tried it with LEFT JOIN as below. But I cannot get all facilities from facility table.
SELECT property_id
, contract_id
, type_id
, location_id
, beds
, ROUND(price,3) as price
, f.facility_id
, f.name
, pf.facility_id
FROM facility f
LEFT JOIN property_facility pf ON pf.facility_id = f.facility_id AND pf.property_id = 6
LEFT JOIN property p USING(property_id)
WHERE p.property_id = 6
Can anybody tell me how I make this query correctly?
Your WHERE clause is filtering out the facilities that do not match. You will need to switch the USING to ON and do:
FROM facility f LEFT JOIN
property_facility pf
ON pf.facility_id = f.facility_id AND
pf.property_id = 6 LEFT JOIN
property p
ON p.property_id = pf.property_id AND p.property_id = 6
You seem to understand the concept, because you are using the same condition for the first JOIN.
Well, actually, the property_id doesn't change. So, you can just remove the WHERE clause and continue with USING if you prefer.
A bit of a newbie question, probably an INNER JOIN with an "AS" statement, but I can't figure it out...
This is for a MYSQL based competition app. I want to select the "img_title" for both img_id1 and img_id2. I can't figure out how to do it and still see which title is assigned to the associated _id1 or _id2.
My tables:
competitions
comp_id
img_id1
img_id2
on_deck
img_id
img_title
Desired results:
comp_id | img_id1 | img_title1 |img_id2 | img_title2
You need a join for each image:
SELECT comp.comp_id, img1.img_id, img1.img_title, img2.img_id, img2.img_title
FROM competitions comp
INNER JOIN on_deck img1 ON img1.img_id = comp.img_id1
INNER JOIN on_deck img2 ON img2.img_id = comp.img_id2
LEFT JOIN if img_id1 or img_id2 can be NULL.
select comp_id, img_id1, b.img_title as img_title1,
img_id2, b2.img_title as img_title2
from competitions a
left outer join on_deck b on b.img_id = a.img_id1
left outer join on_deck b2 on b2.img_id = a.img_id2
switch let outer join to inner join if you want to exclude rows in competitions that do not have two matching img_ids
This query should give you the results you want. This also assumes that comp.img_id1 and comp.img_id2 are never NULL.
SELECT comp.comp_id
, comp.img_id1
, deck1.img_title AS img_title1
, comp.img_id2
, deck2.img_title AS img_title2
FROM competitions AS comp
JOIN on_deck deck1 ON comp.img_id1 = deck1.img_id
JOIN on_deck deck2 ON comp.img_id2 = deck2.img_id
If you have plan on having a NULL or empty string comp.img_id1 and/or comp.img_id2 fields, you'll need to do some left joins.
I'm having trouble with a simple MySQL Query.
Here is the query:
SELECT distinct e.E_CODE, s.S_CODE, p.P_ID, p.P_NAME, p.P_FIRSTNAME, p.P_STATUS, e.E_BOSS, tp.TP_TITLE
from event_participation ep, worker p, type_participation tp, event e, section s
where ep.P_ID = p.P_ID
and s.S_ID = e.S_ID
and ep.TP_ID = tp.TP_ID
and e.E_CODE = ep.E_CODE
The problem is that ep.TP_ID sometimes has a value set to zero while tp.TP_ID has nothing with a zero ID. It's auto-increment and starts at 1 and so on.
The result is obviously that this query does not return records when the ep.TP_ID = 0 and there is no match in tp.TP_ID.
So I'm trying to figure out a way to get those results in there anyway. I was thinking of using a LEFT JOIN statement but couldn't figure out a proper way to insert it into the query.
Any advice on this matter would be greatly appreciated.
First of all, I advice you to use some general type for event_participation records without type; But, unless to take that decision, supposing you want to get all matching records between all tables but also get results with no type, you can use the following query:
SELECT DISTINCT e.E_CODE, s.S_CODE, p.P_ID, p.P_NAME, p.P_FIRSTNAME, p.P_STATUS, e.E_BOSS, tp.TP_TITLE
FROM event_participation ep
JOIN worker p ON (ep.P_ID = p.P_ID)
JOIN event e ON (e.E_CODE = ep.E_CODE)
JOIN section s ON (s.S_ID = e.S_ID)
LEFT JOIN type_participation tp ON (ep.TP_ID = tp.TP_ID)
SELECT DISTINCT e.E_CODE
, s.S_CODE
, p.P_ID
, p.P_NAME
, p.P_FIRSTNAME
, p.P_STATUS
, e.E_BOSS
, tp.TP_TITLE
FROM event_participation ep
JOIN worker p
ON p.P_ID = ep.P_ID
JOIN event e
ON e.E_CODE = ep.E_CODE
JOIN section s
ON s.S_ID = e.S_ID
LEFT
JOIN type_participation tp
ON tp.TP_ID = ep.TP_ID;
I have a simple table with 3 columns
PageDescription(varchar(50)) | ParentID (int) | CategoryID (int)
Here is the table data
Fruit||1
Apples|1|2
here is the query i'm running
SELECT PageDescription,(SELECT PageDescription
FROM tblpages a
WHERE a.ParentID = tblpages.CategoryID)
AS 'Page Parent'
FROM tblpages
This query does not stop processing, is there a better way to handle this query?
You can use a JOIN to perform this:
SELECT t1.PageDescription
, t2.PageDescription as PageParent
FROM t t1
LEFT JOIN t t2
on t1.ParentId = t2.CategoryId
See SQL Fiddle with Demo
Using a LEFT JOIN will allow you to include the items that have a null value.
What you need is a join.
SELECT a.PageDescription, b.PageDescription as Parent
FROM tblpages a
LEFT JOIN tblpages b ON a.CategoryID = b.ParentID
Try to name yout two tables:
SELECT b.PageDescription,(SELECT PageDescription
FROM tblpages a
WHERE a.ParentID = b.CategoryID)
AS 'Page Parent'
FROM tblpages b
SELECT p.PageDescription,p1.PageDescription
FROM tblpages p1
LEFT JOIN p2 on p1.parentId=p2.categoryID
WHERE 1;
am having a problem constructing a query
here is simplified tables structure
3 tables
Event [Event_id , Event_name]
Event_files [Event_id(FK) , File_id(FK)]
Uploaded_Files[File_id , File_type, File_path]
we mainly have 2 file types
image = 2
document = 4
what am trying to do is to get the events along with their images (if they have an image )
am trying to do this with this Query
select e.id, e.name,uf.id as file_id,uf.path
from event e
left join event_file ef on ef.event_id = e.id
left join uploaded_file uf ON ef.file_id = uf.id
i know that i need to apply a condition but each time i do in the where or ON there is always problem with the Query
for example if i apply :
left join uploaded_file uf ON ef.file_id = uf.id AND (uf.type = 2 )
it will still return 2 records for the events that has both image and file one of them with file_path null .
on the other hand if i do the following :
where (uf.id is null OR (uf.id is not null AND uf.type=2))
the events with only files and no image will not be returned any more
is there is solution please ?
thanks in advance
SELECT e.id, e.name, f.file_id AS file_id, f.path
FROM event e
LEFT JOIN
(
SELECT ef.event_id, uf.id AS file_id, uf.path
FROM event_file ef
INNER JOIN uploaded_file uf ON ef.file_id = uf.id AND uf.type = 2
) f ON f.event_id = e.id
This should do (untested.)
The reason you're getting the empty record is because you only specify the uf.type condition on the uploaded_file table, which imposes nothing on the left join for event_file.