Mysql intersection, comparison opposite to UNION?

I am trying to compare two sets of attempts with great difficulty to undesrtand how subqueries work and if they are effective. I am not going to explain all my tables, but just think that I have apar arrays ... I can do this in php, but I wonder if I can do this in mysql right away ...

this is my request to check how many items user 1 has in his lists

SELECT DISTINCT *
FROM list_tb 
INNER JOIN item_to_list_tb 
ON list_tb.list_id = item_to_list_tb.list_id
WHERE list_tb.user_id = 1
ORDER BY item_to_list_tb.item_id DESC 

this is my request to check how many items user 2 has in his lists

SELECT DISTINCT *
FROM list_tb 
INNER JOIN item_to_list_tb 
ON list_tb.list_id = item_to_list_tb.list_id
WHERE list_tb.user_id = 1
ORDER BY item_to_list_tb.item_id DESC 

now the problem is that I would cross these results to check how many item_id elements they have in common ...

thank!!!

+3
source share
1

, MySQL Intersect. , List_Tb.UserId Select Group By, User_Id:

Select ... -- everything except List_Tb.UserId
From List_Tb 
    Inner Join Item_To_List_Tb 
        On List_Tb.List_Id = Item_To_List_Tb.List_Id
Where List_Tb.User_Id In(1,2)
Group By ... -- everything except List_Tb.UserId
Having Count( Distinct List_Tb.User_Id ) = 2
Order By item_to_list_tb.item_id Desc

, , , .

+2

Source: https://habr.com/ru/post/1796115/


All Articles