Performing JOIN between two tables - mysql

I have two tables like this:
table_1
id user_id item_id item_number
15 1 1 7
16 1 2 12
17 1 3 1
18 1 4 0
19 1 5 11
20 5 1 2
21 5 2 2
22 5 3 5
23 5 4 7
24 5 5 1
table_2
id user_id item_id attribute
41 5 1 1
42 1 1 1
43 7 5 1
44 1 4 1
45 1 4 0
I would like to select user_id, item_id and item_number from table_1 and number of rows from table_2 where table_1.user_id = table_2.user_id and table_1.item_id = table_2.item_id.
I have this:
SELECT item_id, item_number, COUNT(attribute) AS number
FROM table_1
LEFT OUTER JOIN table_2 ON table_1.user_id = table_2.user_id
WHERE table_1.user_id='1'
Can you help me?
Expected result:
user_id item_id item_number number
1 1 7 1
1 2 12 0
1 3 1 0
1 4 0 2
1 5 11 0

Try this:
SELECT t1.user_id, t1.item_id, t1.item_number, COUNT(attribute) AS number
FROM table_1 t1
LEFT OUTER JOIN table_2 t2 ON t1.user_id = t2.user_id AND t1.item_id = t2.item_id
WHERE t1.user_id = '1'
GROUP BY t1.user_id, t1.item_id;

you have to use alias as same column names confuses the server to identify which column have to fetch..so you can use this..
SELECT t1.user_id,t1.item_id, t1.item_number, COUNT(attribute) AS number
FROM table_1 t1
LEFT OUTER JOIN table_2 t2 ON t1.user_id = t2.user_id
WHERE t1.user_id='1' GROUP BY t1.user_id, t1.item_id;

Since you don't really need any column values from table_2, I'd go with a sub-query to get the count:
select user_id, item_id, item_number,
(select count(*) from table_2
where table_1.user_id = table_2.user_id
and table_1.item_id = table_2.item_id)
from table_1
WHERE table_1.user_id='1'

Related

Finding the mutual interests

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

SQL query for below data

For input, When quantity value greater then 1, convert in a new row with value 1 for quantity column.
INPUT
ID ProductFK Quantity Price
------------------------------------------------
10 1 2 100
11 2 3 150
12 1 1 120
OUTPUT
ID ProductFK Quantity Price
------------------------------------------------
10 1 1 100
10 1 1 100
11 2 1 150
11 2 1 150
11 2 1 150
12 1 1 120
We can do this using a sequence table trick. Inner join your current table to a sequence on the condition that the quantity be greater than or equal to the sequence value. For example:
SELECT t1.ID, t1.ProductFK, 1 AS Quantity, t1.Price
FROM yourTable t1
INNER JOIN (SELECT 1 AS Quantity UNION ALL SELECT 2 UNION ALL SELECT 3) t2
ON t1.Quantity >= t2.Quantity
ORDER BY t1.ID;
Demo

Group and calculate sum with joined table

I have table1 like this:
ID uid jid type val
1 1 1 1 1
2 1 1 1 10
3 1 2 1 100
4 1 3 1 1000
5 1 4 2 2
And joined table2:
ID uid jid stat time
1 1 1 1 100
2 1 1 1 200
3 1 1 4 300
4 1 2 2 400
I try to get SUM of val from table1, group it by uid and type and join table2 by this query:
SELECT a.uid, a.type, SUM(a.val) as t1, SUM(a.val)*COUNT(distinct(a.id))/COUNT(a.id) as t2, MAX(b.time) as max_time
FROM table1 as a
LEFT JOIN table2 as b on b.uid = a.uid and b.jid = a.jid and b.stat = 1
GROUP BY a.uid, a.type
In result I get this values:
uid type t1 t2 max_time
1 1 1122 748.0000 200
1 2 2 2.0000 NULL
But total for type=1 should be: 1111 (not 1122 and not 748)
Please tell me what I'm doing wrong.
You need use DISTINCT with a.uid in select statement. corrected query is below:
SELECT DISTINCT(a.uid), a.type, SUM(a.val) AS t1, SUM(a.val)*COUNT(DISTINCT(a.id))/COUNT(a.id) AS t2, MAX(b.time) AS max_time
FROM table1 AS a
LEFT JOIN table2 AS b ON b.uid = a.uid AND b.jid = a.jid AND b.stat = 1
GROUP BY a.uid, a.type
can you recheck the sql:
SELECT auid, atype, SUM(aval)
, SUM(aval)*COUNT(DISTINCT(aid))/COUNT(aid) AS t2, MAX(btime) AS max_time
FROM
(
SELECT DISTINCT(a.uid) AS auid, a.type AS atype, a.val AS aval, a.id AS aid, b.time AS btime
FROM table1 AS a
LEFT JOIN table2 AS b ON b.uid = a.uid AND b.jid = a.jid AND b.stat = 1
) AS grouptable
GROUP BY auid, atype
Output I got:
auid atype sum(aval) t2 max_time
1 1 1111 1111.0000 200
1 2 2 2.0000 (NULL)
Is the result you are looking for?

MySql get highest count per parent ID

ID item_ID parent_ID count
================================
1 11 2 5
2 12 2 6
3 13 3 2
4 14 3 3
5 15 2 7
6 16 1 3
SELECT * FROM relations ORDER BY count DESC
The row that should be returns are 2,4 and 6 because they have the highest count for their parent_ID
how do i change the query to accomplish this?
The inner select gets the highest count for each parent_ID. If you join against that, it filters out the relevant records
select t1.*
from your_table t1
join
(
select parent_ID, max(count) as mcount
from your_table
group by parent_ID
) t2 on t1.parent_ID = t2.parent_ID
and t1.count = t2.mcount

How to get non-matching records from two tables

See these two sample tables:
Table 1:
id acc_no name
------------------------
1 14 aaaa
2 16 bbbb
3 18 ccccc
4 25 wwww
5 27 xxxxxxx
6 28 zzzzzzz
Table 2:
sr no acc_no amount
----------------------
1 14 2000
2 16 2344
3 18 3200
I need to get records on basis of acc_no which are not matching in table 1 for example:
OUTPUT:
id acc_no name
---------------------
4 25 wwww
5 27 xxxxxxx
6 28 zzzzzzz
When I tried with below query ,the result was not reliable:
SELECT t1.*
FROM table1 t1
LEFT OUTER JOIN table2 t2 ON t1.acc_no = t2.acc_no
WHERE t2.acc_no IS NULL
Give your suggestions. What will be right SQL query ti get above output?
try:
SELECT *
FROM table1 t1
WHERE t1.acc_no NOT IN (SELECT acc_no FROM table2)
Should be :
select t1.id,t1.acc_no,t1.name from table1 t1
left outer join table2 t2 on t1.acc_no = t2.acc_no
where
t2.id is null
Try this one also:
select t1.* from table1 t1 where
not exists (
select 1 from table2 t2
where t1.acc_no=t2.acc_no
)