How to do the minus operation given 2 columns using mysql - mysql

allpairs
user1 user2
1 1
2 1
3 1
1 2
2 2
3 2
1 3
2 3
3 3
likesPairs
user1 user2
1 2
2 1
3 1
I want to do allPairs - likedPairs to get the relation
notliked
user1 user2
1 1
2 2
3 2
1 3
2 3
3 3
I tried something like this but it just errors
select user1, user2
from allpairs NOT IN likespairs

This sounds like not exists:
select ap.*
from allpairs ap
where not exists (
select 1
from likespairs lp
where lp.user1 = ap.user1 and lp.user2 = ap.user2
)

In this case the LEFT JOIN can be used with check for NULL values:
select allpairs.*
from allpairs
left join likespairs
on (allpairs.user1 = likespairs.user1 and allpairs.user2 = likespairs.user2)
where likespairs.user1 is null;
Here you can try the SQL code
If pair's order not a matter you can add next into LEFT JOIN condition:
select allpairs.*
from allpairs
left join likespairs on (
(allpairs.user1 = likespairs.user1 and allpairs.user2 = likespairs.user2) or
(allpairs.user2 = likespairs.user1 and allpairs.user1 = likespairs.user2)
)
where likespairs.user1 is null;
Try SQL here

Related

Select all Groups and check if User is in a Group or not

I have two tables groups and groups_members
groups
id
name
1
GROUP 1
2
GROUP 2
3
GROUP 2
groups_members
group_id
user_id
1
123
2
123
2
555
1
4643
3
45434
Now I want to display ALL groups and check if the user i show the groups for (lets say user_id = 555) is in a group or not. Something like this:
SELECT g.id, g.name, is_in_group
FROM g.groups
JOIN groups_members gm ON gm.user_id = 555
Expected output:
id
name
is_in_group
1
GROUP 1
NULL
2
GROUP 2
1
3
GROUP 3
NULL
You have several ways to do so, a simple trick that comes to my mind is this one:
SELECT
g.*,
CAST(gm.user_id / gm.user_id AS UNSIGNED) AS is_in_group
FROM sgroups AS g
LEFT JOIN sgroups_members AS gm
ON g.id = gm.group_id
AND gm.user_id = 555;
-- id name is_in_group
-- 1 GROUP 1 NULL
-- 2 GROUP 2 1
-- 3 GROUP 3 NULL

SQL query two tables with a condition in the child table and record not exist in child

i have two tables (mysql):
channels:
uid
time
1
23423
2
52422
3
23423
4
42342
NULL
345
users:
uid
id
gid
1
sam1
1
2
sam2
2
3
sam2
2
4
sam2
3
i want to select all channel for users with gid=1 and gid=2 and for not existing users
i run query:
SELECT u.id, u.gid, c.time
FROM channels c
LEFT JOIN users u ON (u.uid=c.uid)
WHERE (u.gid IN (NULL,'1', '2'))
and have
uid
gid
time
1
1
23423
2
2
52422
3
2
23423
how to select a channel for a non-existent user ?
i want to get the following result:
uid
gid
time
1
1
23423
2
2
52422
3
2
23423
NULL
NULL
345
Use IS NULL to check for null values:
SELECT u.id, u.gid, c.time
FROM channels c
LEFT JOIN users u ON u.uid = c.uid
WHERE u.gid IN (1, 2) OR c.uid IS NULL;

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;

UPDATE all, except last

I have a table like this:
table_documents
document_id
document_folder_id
document_title
document_notify_expired
ID FOLDER TITLE Notify Expired
1 2 Test1 1
2 2 Test2 1
3 2 Test3 1
4 2 Test4 1
5 2 Test5 1
I'm like to UPDATE and set document_notify_expired to 0 for all records EXCEPT last, for a specific folder like below
ID FOLDER TITLE Notify Expired
1 2 Test1 0
2 2 Test2 0
3 2 Test3 0
4 2 Test4 0
5 2 Test5 1
Here my code but not update as expected
UPDATE table_documents docs
LEFT OUTER JOIN ( SELECT * FROM table_documents ORDER BY document_id DESC LIMIT 1 )last_doc ON last_doc.document_id = docs.document_id
SET doc.document_notify_expired = '0'
WHERE document_folder_id = '2'
AND last_doc.document_notify_expired = '1'
Try this out
UPDATE table_documents docs
INNER JOIN
(SELECT
MAX(id) id
FROM
table_documents) docsmax ON docs.id != docsmax.id
SET
document_notify_expired = 0;
Obviously the last row has the greatest id, so this row is not going to be there after the join, which will returns all the other rows and you can play with them as you wish.
update table1.table_documents
set table1.document_notify_expired = 0
where table1.document_folder_id = 2
and not table1.document_id = (
select table2.document_id
from table_documents as table2
where table2.document_folder_id = 2
order by table2.document_id desc
limit 1
);

update from select result

I tried to update the color in table tbl of colors in the item table based on field item_id, with the following query. but I get an error Sql error (1242).
update tbl mt
left join tbl_detail dt on mt.tbl_No = dt.tbl_No
set dt.color_Id =
(
select p.color_ID
from ( select dt.item_Id, dt.color_Id
from tbl mt
left join tbl_detail dt on mt.tbl_No = dt.tbl_No
where mt.Tipe = 2 ) h
left join itemp on h.item_id = p.item_id
)
where mt.Tipe = 2 ;
I want to update a field color in the table tbl with existing color in the item table. How I should make it?
I have tried a simple syntax like this but then I get sql error (1442)
update tbl mt
left join tbl_detail dt on mt.tbl_No = dt.tbl_No
left join item p on dt.item_Id = p.item_Id
set dt.color_Id = p.color_id
where mt.Tipe = 2 ;
Example :
table tbl:
|tbl_No|tipe|
1 2
2 2
3 2
table tbl_detail:
|tbl_detail_No|tbl_No|item_id|color_id|
1 1 1 null
2 1 2 null
1 2 3 null
2 2 4 null
table item:
|item_id|color_id|
1 1
2 2
3 3
4 4
I want to update color_id in table tbl_detail based on item_id in table item; after the update, the table tbl_detail should be as follows.
table tbl_detail:
|tbl_detail_No|tbl_No|item_id|color_id|
1 1 1 1
2 1 2 2
1 2 3 3
2 2 4 4
(oh, I'm really bad at explaining it, hope someone can understand as English is not my first language.)
Thanks for all the help and advice.