I have 2 tables
List
id_1 id_2 tym event
1 2 xx 1
1 3 yy 2
2 1 zz 3
Details
id name
1 a
2 b
3 c
I want to get id_1.name, id_2.name, tym, event
Expected result
a b xx 1
a c yy 2
b a zz 3
You need to join the details table twice
select d1.name, d2.name, l.tym, l.event
from List l
left join Details d1 on l.id_1 = d1.id
left join Details d2 on l.id_2 = d2.id
Fiddle Demo
Related
I have three table and structure as below :
abc
id name education
1 test1 a
2 test2 b
3 test3 c
pqr
id name abcid
1 takr 1
2 test21 1
3 testlll 2
xyz
id name abcid
1 takr 2
2 test21 2
3 testlll 3
I want all the data based on abc table id
below is the query
SELECT d.ID,l.VALUE_ID as RI_ID,l.UF_CRM_1486370412 as RI_AMT, f.VALUE_ID as Finance_Id , f.UF_CRM_1595053941 as FI_AMT
FROM b_uts_crm_lead as l
JOIN b_crm_deal as d
ON d.ID = l.UF_CRM_1600342528
JOIN b_uts_crm_financeaddition as f
ON d.ID = f.UF_CRM_1600350766
I am guessing you want left join:
select . . . -- the columns you want
from abc left join
xyz
on xyz.abcid = abc.id left join
pqr
on pqr.abcid = abc.id;
I just want to merge the content of 2 tables and display it based on the ID.
Both the table has 3 entries.
Table a - Sampling order
Date Docname Products Quantity ID
1 A A 1 1
2 B B 2 1
3 C C 3 1
Table B - Representative locations
Date Area lat long ID
1 a 1 1 1
2 b 2 2 1
3 c 3 3 1
The output should generate like 3 rows with all the table A columns and B columns where ID = Specified ID
I need a output like this
Date Docname product Quantity Area lat long
1 A A 1 a 1 1
2 B B 2 b 2 2
3 C C 3 c 3 3
But its generating 9 rows (3*3) and duplicating the numbers of rows present in both the tables.
Its generating
Date Docname product Quantity Area lat long
1 A A 1 a 1 1
2 B B 2 b 2 2
3 C C 3 c 3 3
1 A A 1 a 1 1
2 B B 2 b 2 2
3 C C 3 c 3 3
1 A A 1 a 1 1
2 B B 2 b 2 2
3 C C 3 c 3 3
Combing number of rows in A * B - I just need only 3 rows with respect to ID.
Query -
$Report = DB::table('sampling_order')
->join('representativelocations','representativelocations.representativeid','=','sampling_order.representativeid')
->select('sampling_order.representativeid as representativeid',
'sampling_order.date as date',
'sampling_order.doctor_name as doctor_name',
'sampling_order.products as products',
'sampling_order.quantity as quantity',
'representativelocations.latitude as latitude',
'representativelocations.longitude as longitude',
'representativelocations.area as area')
->whereBetween('sampling_order.date', [$Datefrom, $Dateto])
->where('sampling_order.representativeid','=',$Representativeid)->get();
What I can see, all Id is '1' in two tables,
so it should be 3 * 3 = 9 rows in result.
I guess you may want this:
id product area-A area-B area-C
1____A____1____2_____3
1____B____2____3_____4
if so. you need to join three times.
SELECT * FROM A
LEFT JOIN (SELECT *, area AS area-A FROM B WHERE area = 'a' ) AS B ON B.id = A.id
LEFT JOIN (SELECT *, area AS area-B FROM B WHERE area = 'b' ) AS C ON C.id = A.id
LEFT JOIN (SELECT *, area AS area-C FROM B WHERE area = 'c' ) AS D ON D.id = A.id
Hope this is what you want.
Basing on the requirement, i think you should try joining on date not ID as below. This will return 3 rows as you are expecting.
select so.date,so.Docname,so.products,so.Quantity,rl.Area,rl.lat,rl.long from Sampling_order AS so
INNER JOIN Representative_locations as rl ON so.Date = rl.Date
WHERE so.ID = 1
Change table names and column names as needed.
My understanding of left outer join is,
table1:
id(pk) name(unique_key) address phone
table2:
new_id(pk) name(foreign key) company work-experience
1st table:
1 a x 123
2 b y 234
3 c z 345
2nd table
1 a aaa 2
2 a aab 3
3 b aab 3
if I will do,
select * from table1 left outer join table2 on table1.name=table2.name,
it will give me
1 a x 123 1 a aaa 2
1 a x 123 2 a aab 3
2 b y 345 3 b aab 3
3 c z 345 NULL NULL NULL NULL
Now instead of above result, I want to get all the rows where company is aab. Also , for any entry in 1st table, if there is no corresponding entry in 2nd table then it should give me NULL for all columns in 2nd table.
like this:
1 a x 123 aab 3
2 b y 234 aab 3
3 c z 345 NULL NULL
is the above result possible with left outer join ? If not, How can I get the above result ?
You can simply add the conditions for the second table (right-side table), in the ON part of LEFT JOIN.
SELECT * FROM table1 AS t1
LEFT OUTER JOIN table2 AS t2
ON t1.name = t2.name AND
t2.company = 'aab'
Also, in case of multi-table queries, it is advisable to use Aliasing, for code clarity (enhanced readability), and avoiding unambiguous behaviour.
select t1.name,t1.address,t1.phoneNo,t2.comapnay,t2.workExperiance from table1 as t1
left outer join table2 as t2 on t1.name=t2.name AND
t2.company = 'aab'
I have a table named 'demo' with the following data:
Name Group MX
A XY 1
B YZ 1
B XY 2
C YZ 5
C XY 3
D YZ 2
E YZ 1
E XY 1
I want unique names based on 'MX' maximum value, when the 'MX' for two identical names are equal I need one of them as illustrated below:
Name Group MX
A XY 1
B XY 2
C YZ 5
D YZ 2
E YZ 1 -- or this {E XY 1}
This is my query:
SELECT demo.Name, demo.Group, demo.MX
FROM (
SELECT Name, MAX(MX) AS max_values
FROM demo
GROUP BY Name
) demo2
INNER JOIN demo
ON demo.Name = demo2.Name
AND demo.MX = demo2.max_values
It is working charmingly, but when the two names are identical it displays both as follow:
Name Group MX
A XY 1
B XY 2
C YZ 5
D YZ 2
E YZ 1
E XY 1
What methods do you recommend?
for avoid two value where Name and MX are equals you could use a (fake) aggregation function and group by eg:
SELECT demo.Name, min(demo.Group), demo.MX
FROM (
SELECT Name, MAX(MX) AS max_values
FROM demo
GROUP BY Name
) demo2
INNER JOIN demo ON demo.Name = demo2.Name AND demo.MX = demo2.max_values
GROUP BY demo.Name, demo.MX
I have three tables as following and I try to group by over three elements to display all possible combinations
Play
--------------------------------
id typeId periodId
--------------------------------
1 a 1
2 b 1
3 b 1
4 b 1
5 a 2
6 b 1
7 a 1
8 b 2
Period
-------------
periodId
-------------
1
2
3
Type
-------------
typeId
-------------
a
b
c
I tried this but it doesn't work, I see some NULL values but the group by doesn't work.
SELECT type, p, count(*) as superNiceCount
FROM Play
RIGHT JOIN Period pp ON Play.periodId = Period.periodId
RIGHT JOIN Type tt ON Play.typeId = Type.typeId
GROUP BY tt.typeId, pp.periodId
The expected result would be
-------------------------
type p superNiceCount
-------------------------
a 1 2
a 2 1
a 3 0
b 1 4
b 2 1
b 3 0
c 1 0
c 2 0
c 3 0
How may I achieve that ?
see if this works
SELECT ty.typeId as type, pe.periodId as p, count(pl.id) as superNiceCount
FROM Period pe
CROSS JOIN Type ty
LEFT JOIN Play pl ON (pl.periodId = pe.periodId AND pl.typeId = ty.typeId)
GROUP BY ty.typeId, pe.periodId
if not try
SELECT ty.typeId as type, pe.periodId as p, count(pl.id) as superNiceCount
FROM (
SELECT * FROM
Period pe
CROSS JOIN Type ty
) as t1
LEFT JOIN Play pl ON (pl.periodId = t1.periodId AND pl.typeId = t1.typeId)
GROUP BY ty.typeId, pe.periodId