How to use Inner join on the same detail table repeatedly - mysql

I am trying to use TWICE inner join statement to get reference values in the same detail table.
Master table: bags_tbl
ID ... bagA bagB
1 ... 121 122
2 ... 123 124
3 ... 125 126
Detail table: fruit_tbl
ID ... fruit ...
121 strawbery
122 apple
123 orange
124 raspberry
125 pear
126 pineapple
SELECT
bags_tbl.ID,
bags_tbl.A,
bags_tbl.B,
fruit_tbl.fruit AS bagA_fruit,
fruit_tbl.fruit AS bagB_fruit
FROM
bags_tbl
Inner Join fruit_tbl ON bags_tbl.bagA = fruit_tbl.fruit
Inner Join fruit_tbl ON bags_tbl.bagB = fruit_tbl.fruit
this throw error : no unique table/alias...
How to make SQL statement to get text representation of master table?
thankx a lot

SELECT
bags_tbl.ID,
bags_tbl.A,
bags_tbl.B,
A.fruit AS bagA_fruit,
B.fruit AS bagB_fruit
FROM bags_tbl
Inner Join fruit_tbl A ON bags_tbl.bagA = A.id
Inner Join fruit_tbl B ON bags_tbl.bagB = B.id

Try this. You need to grant a unique alias to each join so SQL knows which one you're referencing in the SELECT clause.
SELECT
bags_tbl.ID,
bags_tbl.A,
bags_tbl.B,
fruitA.fruit AS bagA_fruit,
fruitB.fruit AS bagB_fruit
FROM
bags_tbl
JOIN fruit_tbl fruitA ON bags_tbl.bagA = fruitA.id
JOIN fruit_tbl fruitB ON bags_tbl.bagB = fruitB.id

Related

NOT Exists with Inner Join

Class_List table
Class_List_id
Class_id
341
4
342
4
Lesson_CLass table
Class_List_id
Lesson_id
341
22
342
22
I am trying to work out a NOT EXIST IN query, which finds all the Class_List_ids in the Class_List table that do not exist in the Lesson_CLass table where the Lesson_id is 21 and class ID is 4.
SELECT Class_List.class_id, Class_List.Class_List_id
FROM Class_List WHERE NOT EXISTS (SELECT
Class_List.class_id, Class_List.Class_List_id,
Lesson_CLass.Class_List_id, Lesson_CLass.Lesson_id
FROM Class_List, Lesson_CLass where
(Class_List.Class_List_id=Lesson_CLass.Class_List_id) AND
(Lesson_CLass.Lesson_id=21) AND (Class_List.Class_id=4));
This results in all the Class_List records rather than the ones that don't exist.
You can use a LEFT JOIN for this purpose
SELECT cl.class_id, cl.Class_List_id
FROM Class_List cl
left join Lesson_CLass lc
on cl.Class_List_id = lc.Class_List_id
and (lc.Lesson_id = 21 and cl.Class_id = 4)
where lc.Class_List_ids is null;

SQL Query Help Conditional Join

Say I have three tables as such:
Group Table
OGID
OGC
OGCD
56
300
TAS
81
TA
CAL
Structure Table
OSID
L1D
L2D
44
56
81
Contract
ContractID
44
Im giving the ContractID and I want to create a Table that has the follow:
ContractID
Structure L1D
Structure L2D
Group OGID
Group OGC
Group OGCD
Group OGID
Group OGC
Group OGCD
44
56
81
56
300
TAS
81
TA
CAL
What would be the best way to go about this in SQL?
There is also the problem that L2D can be null and anytime I try to make INNER JOIN statements to join the tables, the NULL ones are ignore.
SELECT
Contract.ContractID, Structure.L1D, Structure L2D, Group.OGID, Group.OGC, Group.OGID, Group2.OGID, Group2.OGC, Group2.OGID,
FROM
(
SELECT Structure.OSID, Structure.L1d, Group.OGID, Group.OGC, Group.OGCD
FROM Structure
INNER JOIN Group
ON (Structure.L1D = Group.OGID)
) T1
INNER JOIN
(
SELECT Structure.OSID, Structure.L1D, Group2.OGID, Group2.OGC, Group2.OGCD
FROM Structure
INNER JOIN Group2
ON (Structure.L2D = Group2.OGID)
) T2
ON (T1.OSID = T2.OSID OR T2.OSID = NULL)
See DBFIDDLE
SELECT
s.OSID,
s.L1D,
s.L2D,
g1.OGID,
g1.OGC,
g1.OGCSD,
g2.OGID,
g2.OGC,
g2.OGCSD
FROM structure s
LEFT JOIN `group` g1 ON g1.OGID = s.L1D
LEFT JOIN `group` g2 ON g2.OGID = s.L2D
The DBFIDDLE also shows what happens when L2D has the value NULL.
P.S. Generally you should not use, or at least try to avoid, Reserved Words as table names, like GROUP.

How to return customers and additional info who have null in second table

T1 Customers
IDZ NAME MEGAID
123 TOM 32132
124 JEK 32323
125 MAX 32342
126 JIZ 32134
T2 Info:
ID CID GUNS STATUS
1 123 3 1
2 124 4 2
3 126 NULL 1
T3 Status:
ID TYPE
1 Active
2 Inactive
IDZ = CID
I need to return NAME, MEGAID and STATUS (Active/Inactive) for everyone who have NULL on GUNS column from INFO table.
I tried this:
SELECT Customers.Name, CustomersMEGAID, Status.TYPE
FROM Customers
LEFT JOIN Customers ON Info.CID=Custoners.IDZ
WHERE Info.Guns= IS NULL;
But thats doesnt work(
Big thanks if someone can help with this
your question is full with errors but here is a query for you:
SELECT Customers.Name, Customers.MEGAID, Status.TYPE
FROM Customers
LEFT JOIN Info ON Customers.IDZ = Info.CID
INNER JOIN Status ON Info.STATUS = Status.ID
WHERE Info.Guns IS NULL;
You can join all three tables, and then search for nulls in the column.
For example:
select
c.name,
c.megaid,
s.type
from customers c
join info i on i.cid = c.idz
join status s on s.id = i.status
where i.guns is null

Join multiple tables without duplicate row

I would like to select 3 tables with the code number is 777
Table 1
Employee code Company Name
001 a
002 b
Table 2
Employee code Voucher NO Date Amount
001 123 12-4-14 100
001 456 2-5-14 500
002 789 3 -7 14 300
Table 3
Voucher No Tax amt code
123 50 777
789 100 888
The output should be
Company Name Employee code Voucher No Date Amount Tax amt code
a 001 123 12-4-14 100 50 777
a 001 456 2-5-14 500 null null
but there are duplicate row when apply the query
SELECT DISTINCT
table1.COMPANY_NAME,
table2.EMPLOYEE_CODE,
table2.VOUCHER_NO,
table2.DATE,
table2.AMOUNT,
table3.TAX_AMT,
table3.CODE
FROM table2
INNER JOIN table1 ON (table2.EMPLOYEE_CODE = table1.EMPLOYEE_CODE)
INNER JOIN table3 ON (table2.VOUCHER_NO = table3.VOUCHER_NO )
WHERE table3.CODE = '777'
The output from the above query
Company Name Employee code Voucher No Date Amount Tax amt code
a 001 123 12-4-14 100 50 777
a 001 456 2-5-14 500 **50 777**
I got try to use DISTINCT but since no work well. Please kindly help what problem with my query.
Try this :
SELECT
table1.COMPANY_NAME,
table2.EMPLOYEE_CODE,
table2.VOUCHER_NO,
table2.DATE,
table2.AMOUNT,
table3.TAX_AMT,
table3.CODE
FROM table1
INNER JOIN table2 ON (table2.EMPLOYEE_CODE = table1.EMPLOYEE_CODE)
LEFT JOIN table3 ON (table2.VOUCHER_NO = table3.VOUCHER_NO )
HAVING
(table3.CODE = '777' OR table3.CODE IS NULL)
Leave me a comment if you need something else, or if you have a specific issue.
Your example output wasn't produced by your SQL command since you use WHERE statement to filter.
If you need to output only 777 code you should use LEFT JOIN with condition instead of WHERE
.....
FROM table2
INNER JOIN table1 ON (table2.EMPLOYEE_CODE = table1.EMPLOYEE_CODE)
LEFT JOIN table3 ON (table2.VOUCHER_NO = table3.VOUCHER_NO )
AND (table3.CODE = '777')
Your second row in your output maybe because it has the same EMPLOYEE_CODE in table2, so I think this may the query that you want:
SELECT
table1.COMPANY_NAME,
table2.EMPLOYEE_CODE,
table2.VOUCHER_NO,
table2.DATE,
table2.AMOUNT,
table3.TAX_AMT,
table3.CODE
FROM table2
INNER JOIN table1 ON (table2.EMPLOYEE_CODE = table1.EMPLOYEE_CODE)
LEFT JOIN table3 ON (table2.VOUCHER_NO = table3.VOUCHER_NO )
WHERE table1.EMPLOYEE_CODE IN
(SELECT table2.EMPLOYEE_CODE
FROM table2 INNER JOIN table3 ON (table2.VOUCHER_NO = table3.VOUCHER_NO)
WHERE table3.code = '777')

join or select in on multiple fields

(The example that follows is hypothetical, but illustrates the concept).
Using MySQL, say I have 2 tables:
userFromID userToId moreInfo
1 2 cat
1 3 dog
4 1 bear
3 4 fish
And...
userId someInfo addlInfo
1 m 32
2 f 33
3 m 25
4 f 28
And I want to query for a user id, and get back joined info from both tables for all users that share a relationship with user1.
assume that the first table has something like alter table thatFirstTable add unique index(userFromId, userToId) so there won't be any duplicates - each relationship between the two ids will be unique.
it doesn't matter who's the "from" or "to"
so the desired result would be something like this, if queried for relationships with user id: 1
userId moreInfo someInfo addlInfo
2 cat f 33
3 dog m 25
4 bear f 28
Thanks.
/EDIT this "works" but I suspect there's a better way?
SELECT * FROM users JOIN friends ON friends.userFrom = users.id OR friends.userTo = users.id WHERE users.id != 1 AND friends.userFrom = 1 OR friends.userTo = 1
/EDIT2 - I updated the sample output to better reflect the goal
try this query::
select tbl2.userid,tbl1.moreinfo,
tbl2.someinfo,tbl2.addinfo
from tbl1 join tbl2
on (tbl1.usertoid = tbl2.userid and tbl1.userfromid = 1)
You should just join the tables with the query below.
select u.userId, f.moreInfo, u.someInfo, u.addlInfo
from users AS u INNER JOIN friends AS f ON u.userId = f.UserToId
where f.userFrom = 1
Try this. Tested and 100% working
select a.userToID, a.moreInfo, b.someInfo, b.addInfo from tbl1 a
left outer join
tbl2 b on a.userToID = b.userId
where a.userFromID = 1;