i have 2 table with this structure
Products
id title
-----------------
1 sample 1
2 sample 2
3 sample 3
4 sample 4
5 sample 5
6 sample 6
gallery
id typeid name
-------------------------------
1 1 sample for 1
2 1 sample for 1
3 1 sample for 1
4 2 sample for 2
5 2 sample for 2
7 2 sample for 2
8 3 sample for 3
9 3 sample for 3
10 3 sample for 3
11 4 sample for 4
12 4 sample for 4
13 5 sample for 5
14 5 sample for 5
and iwant this for lists of id eg(1,2,3)
id typeid name
---------------------
1 1 sample for 1
1 2 sample for 1
2 4 sample for 2
2 5 sample for 2
3 8 sample for 3
3 9 sample for 3
here is my query
select p.*,g.* from products p inner join gallery g ON p.id=g.typeid where p.id in (3,4,5) group by typeid
here is real structure sqlfiddle link
SELECT p.id, g.typeid, g.id, g.title
FROM products p
INNER JOIN (SELECT * FROM gallery a
WHERE (SELECT COUNT(*) FROM gallery b WHERE b.title = a.title AND b.id >= a.id) <= 2
) g ON p.id = g.typeid
WHERE p.id in (3,4,5)
EDIT:
Try this SQL Fiddle Demo
SELECT p.id, g.typeid, g.id, g.name
FROM products p
INNER JOIN (SELECT * FROM gallery a
WHERE (SELECT COUNT(*) FROM gallery b WHERE b.typeid = a.typeid AND b.id >= a.id) <= 2
) g ON p.id = g.typeid
WHERE p.id in (3,4,5) order by g.id asc
So basically this part
WHERE (SELECT COUNT(*) FROM gallery b WHERE b.title = a.title AND b.id >= a.id) <= 2
is used to replace group by typeid
SELECT p.id,p.title, g.typeid, g.id, g.name
FROM products p
INNER JOIN (SELECT * FROM gallery a
WHERE (SELECT COUNT(*) FROM gallery b WHERE b.typeid = a.typeid AND b.id >= a.id)
please try this
Related
Users Table
id
---
1
2
3
4
5
6
Interests Table
user_id interest_id
-------- -----------
1 34
2 34
3 34
2 45
1 45
3 46
4 47
5 34
6 78
Expected Result (say I am the user with the id of 1)
user_id mutual_interest_count
-------- ---------------------
2 2
3 1
4 0
5 1
6 0
How can I build the query finding the mutual interests?
We can use a self join with aggregation approach here:
SELECT u.id, COALESCE(t.cnt, 0) AS mutual_interest_count
FROM Users u
LEFT JOIN
(
SELECT t2.user_id, COUNT(*) AS cnt
FROM Interests t1
INNER JOIN Interests t2 ON t2.interest_id = t1.interest_id
WHERE t1.user_id = 1 AND t2.user_id <> 1
GROUP BY t2.user_id
) t
ON t.user_id = u.id
WHERE u.id <> 1;
Here is a working demo on MySQL 8.
SELECT Users.id, COUNT(t2.user_id) mutual_interest_count
FROM Users
LEFT JOIN Interests t1 ON t1.user_id = Users.id
LEFT JOIN Interests t2 ON t1.interest_id = t2.interest_id
AND t2.user_id = 1
WHERE Users.id <> 1
GROUP BY Users.id
https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=ea982860620f489855edca217c0169ae
I have 3 tables in which all 3 tables are joined to get the result.
Below is the table,
//tbl_order
order_id order_no_first order_no order_no_last order_date
---------------------------------------------------------------------
1 C 1000 a 2017-05-16
2 C 1001 a 2017-05-16
3 C 1001 b 2017-05-16
4 A 1002 a 2017-05-16
5 A 1002 b 2017-05-16
//tbl_assign
assign_id order_id order_no_first central_status central_assign_unit
----------------------------------------------------------------------------
1 1 C 1 1
2 2 C 1 1
3 3 C 1 1
4 4 A 1 1
//tbl_unit_status
status_id assign_id status_status
---------------------------------------
1 1 Cutter
2 1 Stitch
3 1 Delivery
4 2 Cutter
5 2 Stitch
6 3 Cutter
7 4 Cutter
I want the result as below,
//Required output
order_id assign_id order_no_first order_no order_no_last status_status
---------------------------------------------------------------------------
2 2 C 1001 a Stitch
3 3 C 1001 b Cutter
4 4 A 1002 a Cutter
5 A 1002 b
from the table tbl_unit_status the status below status_status field, if it is Despatch then do not display that result.
I have tried to get the above result. But no success below is my code.
`SELECT *
FROM tbl_order o
LEFT JOIN tbl_assign a
ON a.order_id = o.order_id AND o.order_no_first = a.order_no_first
LEFT JOIN (
SELECT u.assign_id, max(u.status_id) AS maxid
FROM tbl_unit_status u GROUP BY u.assign_id) uu
ON uu.assign_id = a.assign_id
LEFT JOIN tbl_unit_status u2 on u2.status_id = uu.maxid
WHERE a.central_status = 1 AND a.central_assign_unit = 1
OR (u2.status_status != "Delivery" AND u2.status_status != "Despatch")
GROUP BY o.order_id
From the above code, the result is
//wrong output
order_id assign_id order_no_first order_no order_no_last status_status
---------------------------------------------------------------------------
1 1 C 1000 a Delivery
2 2 C 1001 a Stitch
3 3 C 1001 b Cutter
4 4 A 1002 a Cutter
5 A 1002 b
Is there any way to get the Required output as soon in the first output. I have tried and am stuck in here.
Thank you.
Use parenthesis correctly arround AND OR clauses and I recommend using NOT LIKE instead of != when dealing with strings.
SELECT *
FROM tbl_order o
LEFT JOIN tbl_assign a
ON a.order_id = o.order_id AND o.order_no_first = a.order_no_first
LEFT JOIN (
SELECT u.assign_id, max(u.status_id) AS maxid
FROM tbl_unit_status u GROUP BY u.assign_id) uu
ON uu.assign_id = a.assign_id
LEFT JOIN tbl_unit_status u2 on u2.status_id = uu.maxid
WHERE a.central_status = 1 AND (a.central_assign_unit = 1 OR u2.status_status NOT LIKE 'Delivery' AND u2.status_status NOT LIKE 'Despatch')
GROUP BY o.order_id
Obs: I can't comment due to my reputation, I'm sorry
You should be using AND instead of OR in your where clause. You don't need the parentheses either.
SELECT *
FROM tbl_order o
LEFT JOIN tbl_assign a
ON a.order_id = o.order_id
AND o.order_no_first = a.order_no_first
LEFT JOIN
(SELECT u.assign_id, max(u.status_id) AS maxid
FROM tbl_unit_status u
GROUP BY u.assign_id
) uu ON uu.assign_id = a.assign_id
LEFT JOIN tbl_unit_status u2
on u2.status_id = uu.maxid
WHERE a.central_status = 1
AND a.central_assign_unit = 1
AND (
u2.status_status IS NULL
OR
u2.status_status NOT IN ("Delivery", "Despatch")
)
GROUP BY o.order_id
I have join with a multiple tables. You can find the tables and MySQL query in this demo.
These are the tables:
users
id name
1 abc
2 xyz
3 pqr
friend
user_id friend_id
1 2
1 3
2 3
collection
id user_id friend_id amount
1 1 2 100
2 2 1 -100
3 2 3 200
4 3 2 -200
5 1 3 300
6 3 1 -300
bill
id use_id(bill_creator)
1 1
2 2
3 2
bill_person
id bill_id user_id
1 1 1
2 1 2
3 1 3
4 2 2
5 2 3
6 3 2
6 3 1
So far I have made this query:
SELECT mf.id
, mf.name
, c.amount AS amount,count(bp.user_id) AS no_common_bills
FROM (
SELECT fr.user_id AS user_id
, fr.friend_id AS friend_id
FROM friend fr
JOIN users fru
ON fru.id = fr.user_id
WHERE fru.id IN (1)
UNION
SELECT fl.friend_id AS user_id
, fl.user_id AS friend_id
FROM friend fl
JOIN users flf
ON flf.id = fl.friend_id
WHERE flf.id IN (1)
) f
JOIN users mf
ON mf.id = f.friend_id
LEFT
JOIN collection c
ON c.friend_id = mf.id
AND c.user_id = f.user_id
LEFT JOIN bill_person bp
ON bp.user_id=f.user_id AND c.friend_id = mf.id
GROUP BY mf.id
ORDER BY mf.id
and my output of this query is
id NAME AMOUNT NO_COMMON_BILLS
2 XYZ 100 2
3 PQR 300 2
but I want this result:
id NAME AMOUNT NO_COMMON_BILLS
2 XYZ 100 2
3 PQR 300 1
I am getting wrong output at NO_COMMON_BILLS. Other than that, all values are correct.
Third time's a charm...
http://sqlfiddle.com/#!2/338dd/12
SELECT u.name friend
, COUNT(bp2.user_id) no_common_bills
FROM users u
LEFT
JOIN
( SELECT user_id me, friend_id them FROM friend WHERE user_id = 1
UNION
SELECT friend_id,user_id FROM friend WHERE friend_id = 1
) x
ON x.them = u.id
LEFT
JOIN bill_person bp1
ON bp1.user_id = x.them
LEFT
JOIN bill_person bp2
ON bp2.bill_id = bp1.bill_id
AND bp2.user_id = x.me
WHERE u.id <> 1
GROUP
BY friend;
FRIEND NO_COMMON_BILLS
jkl 0
mno 0
pqr 1
xyz 2
I have 2 tables:
users:
id | name | club_id |
1 Bob 4
2 Jane 5
3 Alex 4
4 Paul 4
5 Tom 4
points:
user_id | club_id | amount(can vary)
1 4 10
1 2 10
2 5 10
3 4 10
3 4 10
4 4 10
3 2 10
3 4 10
I need (where users.club_id = 4 AND points.club_id = 4):
user_id | name | sum(amount)
3 Alex 30
1 Bob 10
4 Paul 10
5 Tom 0
Notice Tom is present in users but doesn't have any entries in points, so his sum should be 0. This is what throws me off in conjuction with grabbing a list from users.
Also would like this to be as efficient as possible (hence I added club_id = 4 both in users and points)
Try this:
SELECT
u.id,
u.name,
COALESCE(SUM(p.amount), 0) AS Totalpoints
FROM
(
SELECT *
FROM users
WHERE club_id = 4
) AS u
LEFT JOIN
(
SELECT *
FROM points
WHERE club_id = 4
) AS p ON p.user_id = u.id
GROUP BY u.id,
u.name;
See it in action here:
SQL Fiddle Demo
try this query
select u.id, u.name, sum(if (p.amount is null, 0, p.amount)) as totalPoint
from
user u
left join
(select * from point p where p.club_id = 4)p
on u.id = p.user_id
where u.club_id=4
group by u.id
SQL FIDDLE:
I have following 3 tables where B, C have many to one relation ship with A, and i need to generate the following Result table with MySql
Table A Table B Table C
id name id A.id qty id A.id
1 a 1 1 5 1 1
2 b 2 1 10 2 1
3 c 3 2 4 3 2
4 3 6 4 2
5 2 7
Expected Result Table
C.id A.id sum(B.qty Group by A.id)
1 1 15
2 1 15
3 2 11
4 2 11
select c.id, a.id, sum(b.qty)
from c
inner join a on c.a_id = a.id
inner join b on b.a_id = a.id
group by c.id, a.id
or in your case, you just don't need table a
select c.id as cid, c.a_id as aid , sum(b.qty) as totalqty
from c
inner join b on b.a_id =c.a_id
group by c.id, c.a_id;