Joining the table creating a duplicate entry in laravel - mysql

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.

Related

How to subtract data from table2 to table1

I'm trying to subtract and add existing values in table like this
Table 1
id q q_out
1 10 0
2 10 0
Table 2
id q
1 2
1 1
2 1
2 2
I am expecting this output when i update table 1:
id q q_out
1 7 3
2 7 3
but I get this output:
id q q_out
1 8 2
2 9 1
this is my query:
UPDATE
db_pro d
JOIN cart c ON d.pro_num = c.p_num
SET
d.q = (d.q - c.q),
d.out_q = (d.out_q + c.q)
WHERE
c.s_num='13-37478' and
c.class not like 'Books' and
c.remarks like 'On Process'
You need to join the agregated values of table cart:
UPDATE
db_pro d
JOIN (
select p_num, sum(q) q from cart
WHERE
s_num='13-37478' and
class not like 'Books' and
remarks like 'On Process'
group by p_num
) c ON d.pro_num = c.p_num
SET
d.q = (d.q - c.q),
d.out_q = (d.out_q + c.q)

SQL Joining between 2 table [multi column to single column]

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

sql group by although there is not value

I have a table where exists 4 entries like this.
class_type
id type
1 A
2 B
3 M
4 T
and another table where these values are foreign key.
id number id_class_type
1 10 1
2 11 1
3 12 2
4 13 1
5 14 2
6 15 3
7 16 1
8 17 3
So what i want is count(*) and group by id_class_type but with all class_type (1,2,3,4) although there is not present the id 4.
if you want only the class tha match you can use inner join
select a.class_type, count(*)
from class_type a
inner join table2 b on a.id = b.id_class_type
group by a.class_type
otherwise you can use left join
select a.class_type, count(*)
from class_type a
left join table2 b on a.id = b.id_class_type
group by a.class_type

get count(*) from multiple tables sql

I have three tables.
entry
ID title
1 Entry1
2 Entry2
3 Entry3
4 Entry4
user_likes
ID user_id entry_id
1 1 3
2 3 1
3 9 4
4 2 2
user_bookmarks
ID user_id entry_id
1 6 3
2 4 3
3 2 1
4 2 2
What i want is the sum of likes and bookmarks for each entry.
result
entryID likes bookmarks
1 1 1
2 1 1
3 1 2
4 1 0
Also with total sum of likes and bookmarks of each entry.
result2
entryID likes+bookmarks
1 2
2 2
3 3
4 1
I managed to get likes and bookmark result using this query in seperate tables. I was not able to show them together in a single table.
SELECT entry.id, COUNT(entry.id) AS likes FROM entry
INNER JOIN user_like ON user_like.entry_id = entry.id GROUP BY entry.id ORDER BY likes DESC
You should aggregate before joining:
select e.*, coalesce(l.likes, 0) as likes,
coalesce(b.bookmarks, 0) as bookmarks,
(coalesce(l.likes, 0) + coalesce(b.bookmarks, 0)) as both
from entries e left join
(select entryid, count(*) as likes
from likes l
group by entryid
) l
on l.entryid = e.id left join
(select entryid, count(*) as bookmarks
from bookmarks
group by entryid
) b
on b.entryid = e.id;

group by over three elements to display all possible combinations

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