I have 2 tables to denote a conversation, I think it is a pretty standard format.
CREATE TABLE conversation (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(40) DEFAULT '',
last_modified TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(id)
) ENGINE = InnoDB;
CREATE TABLE conversation_user (
user_id INT NOT NULL,
conversation_id INT NOT NULL,
conversation_is_visible BOOLEAN NOT NULL DEFAULT TRUE,
unread BOOLEAN NOT NULL DEFAULT TRUE,
PRIMARY KEY (user_id, conversation_id),
CONSTRAINT fk_conversation_user_user_id FOREIGN KEY (user_id) REFERENCES user (id),
CONSTRAINT fk_conversation_user_conversation_id FOREIGN KEY (conversation_id) REFERENCES conversation (id)
) ENGINE = InnoDB;
I want to be able to check if a conversation exists between 2 users (I don't care about starting a new conversation for more than 2 users if it already exists).
I can't seem to be able to write a query to select the conversation with just the 2 given users in it, and no more.
I then want to convert the query into a hibernate query.
For example if i had the following data:
select * from conversation;
+----+------------------------+---------------------+
| id | name | last_modified |
+----+------------------------+---------------------+
| 1 | test conversation 1 | 2017-02-25 15:39:01 |
| 2 | test conversation 2 | 2017-02-26 16:29:14 |
+----+------------------------+---------------------+
select * from conversation_user;
+---------+-----------------+-------------------------+--------+
| user_id | conversation_id | conversation_is_visible | unread |
+---------+-----------------+-------------------------+--------+
| 1 | 1 | 1 | 0 |
| 1 | 2 | 1 | 0 |
| 2 | 1 | 1 | 1 |
| 2 | 2 | 1 | 0 |
| 3 | 2 | 1 | 0 |
+---------+-----------------+-------------------------+--------+
The query should when searching for a conversation between user 1 and user 2 only have conversation 1 show up, as conversation 2 has user 3 in it aswell.
I've tried the following query, but that shows up both conversations, so not sure how to restrict it to conversations just containing those users...
SELECT * FROM conversation
LEFT JOIN conversation_user ON conversation.id = conversation_user.conversation_id
WHERE user_id IN (1,2);
I could use this query in hibernate and then check the set of conversation users in the conversation to make sure the conversation only contains the 2 given users, but was wondering if there was a more efficient approach
Try this
http://sqlfiddle.com/#!9/587e4/5
SELECT t1.user_id,
t1.conversation_id,
t1.conversation_is_visible,
t1.unread
FROM conversation_user t1
LEFT OUTER JOIN
(SELECT user_id,conversation_id
FROM conversation_user WHERE user_id IN (1,2)
) t2 ON t1.user_id=t2.user_id
AND t1.conversation_id=t2.conversation_id
WHERE t2.conversation_id IS NOT NULL;
If you just want conversation_id then use distinct t1.conversation_id in select clause.
Based on Utsav's answer, the following query got the results i was after.
SELECT t1.user_id, t1.conversation_id, t1.conversation_is_visible, t1.unread
FROM conversation_user t1
LEFT OUTER JOIN
(SELECT *
FROM conversation_user
GROUP BY conversation_id HAVING COUNT(*) = 2
) t2
ON t1.user_id = t2.user_id AND t1.conversation_id=t2.conversation_id
WHERE t1.user_id IN (1,2) AND t2.conversation_id IS NOT NULL;
still not sure how to convert this to a query in hibernate but its a start
Related
I have two tables s_filter_values and s_filter_articles.
s_filter_values: s_filter_articles:
| id | value | | articleID | valueID |
|----|-------| |-----------|---------|
| 1 | one | | 1 | 2 |
| 2 | two | | 1 | 3 |
| 3 | three | | 2 | 2 |
With the following statement I count the the occurence of the values
respectively I get the values that are not linked to an article:
SELECT v.*, IFNULL(COUNT(a.articleID), 0) AS counter
FROM s_filter_values AS v
LEFT JOIN s_filter_articles AS a ON v.id = a.valueID
GROUP BY v.id
HAVING counter = 0
In this case, I got
| id | value | counter |
| 1 | one | 0 |
My questions is: How can I use this statement to delete all rows from s_filter_values that are not linked to an article?
I will suggest using NOT EXIST rather than NOT IN.
The NOT EXISTS should perform faster on a large dataset.
There is one key difference between the two constructs: if the subquery returns a NULL in its results then the NOT IN condition will fail, because null is neither equal-to nor not-equal-to any other value.
create table s_filter_values(
id int(10),
`value` varchar(10) );
insert into s_filter_values values ( 1,'one'),( 2,'two'),( 3,'three');
create table s_filter_articles(
articleID int(10),
valueID int(10) );
insert into s_filter_articles values ( 1,2),( 1,3),( 2,2);
DELETE FROM s_filter_values
WHERE NOT EXISTS (SELECT valueID FROM s_filter_articles a where a.valueID= s_filter_values.id);
Demo: https://www.db-fiddle.com/f/7yUJcuMJPncBBnrExKbzYz/84
I would use a subquery to get all IDs that are in the table. Then drop the rows from s_filter_values that are not present.
DELETE FROM s_filter_values WHERE id NOT IN (SELECT DISTINCT valueID FROM s_filter_articles);
A simple sub select should do it:
DELETE FROM `s_filter_values`
WHERE `id` NOT IN SELECT DISTINCT `valueID` FROM `s_filter_articles`
Two tables users, relationships in my db.
CREATE TABLE users(
id int primary key auto_increment,
nickname varchar(20),
is_active TINYINT
)
CREATE TABLE relationships(
id int primary key auto_increment,
user_id int,
target_user_id int,
FOREIGN KEY(user_id) REFERENCES users(id),
FOREIGN KEY(target_user_id) REFERENCES users(id)
)
mysql> select * from users;
+----+----------+-----------+
| id | nickname | is_active |
+----+----------+-----------+
| 1 | hide | 1 |
| 2 | john | 1 |
| 3 | ben | 0 |
| 4 | test | 1 |
| 5 | gogo | 1 |
+----+----------+-----------+
mysql> select * from relationships;
+----+---------+----------------+
| id | user_id | target_user_id |
+----+---------+----------------+
| 1 | 1 | 2 |
| 2 | 1 | 4 |
+----+---------+----------------+
I have to extract users.id with certain condition.
I will explain in case of users.id = 1
users.is_active=1
user who does not have relationships via relationships table. you know that in current relationships table, user_id = 1 has 2 rows that target_user_id = 2 and 4. So query result does not contain user_id = 2 and 4.
Using NOT IN, it is pretty simple.
SELECT id FROM users WHERE is_active=1 AND id NOT IN(SELECT target_user_id FROM relationships WHERE user_id=1)
RESULT : 1, 5
Note that there is huge rows in users and relationships.
If I using NOT IN with subquery, it will occur performance issue.
So I think I have to join with foreign key but I don't know how to make query exactly.
Any suggestion, appreciate.
Thanks.
TRY THIS: I am sure LEFT JOIN with IS NULL approach will definitely work for you
SELECT u.id
FROM users u
LEFT JOIN relationships r ON r.target_user_id = u.id
AND r.user_id = 1
WHERE u.is_active=1 AND r.target_user_id IS NULL
Nothing wrong with your query. MySQL should be able to use your index.
However, you can also use left join:
SELECT
users.id
FROM
users
LEFT JOIN relationships ON (
users.id = relationships.target_user_id
/*
-- un-comment if filtering by user is needed
AND relationships.user_id = 1
*/
)
WHERE
users.is_active=1
AND relationships.target_user_id IS NULL
-- addition filtering can be here
UPDATE:
If you filtering by user, you can try to add composite index (user_id, target_user_id) to relationships table (columns should be in this order)
Table1: tbl_users:
+----+--------+
| id | name |
+----+--------+
| 1 | waheed |
+----+--------+
| 2 | fareed |
+----+--------+
Table2: tbl_watched:
+------------+----+--------+
| id_watched | id | name |
+------------+----+--------+
| 1 | 2 | fareed |
+------------+----+--------+
I want to get the total records if the tbl_watched is not present it should return as zero or null
Output:
+----+--------+--------+
| id | name |watched |
+----+--------+--------+
| 1 | waheed | 90 |
+----+--------+--------+
| 2 | fareed | null |
+----+--------+--------+
How can I get this kind of result?
Use LEFT JOIN
Select t1.id , t1.name, count(*) as total from tbl_users as t1
left join tbl_watched as t2 on t1.id=t2.id_watched
group by t1.id , t1.name
You can do that with a LEFT JOIN between users and watched. That will preserve all rows in the left table and will assign NULL if no matching record is available on the right side. On this result set you can GROUP BY the user's name and count how many things each one has seen.
select t1.name, count(t2.id)
from tbl_users t1
left join
tbl_watched t2
on t1.id = t2.watched_id
group by t1.name
You can join the tables. I'm not sure about the id_watched column there, assuming its a foreign key from another table. Created a sample schema and query for you at SqlFiddle. Please notice that the third person does not have any watched content, so the count will be 0 in this case. See here.
Sample Schema:
create table tbl_users (
id INT(8) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(266) NOT NULL
);
create table tbl_movies (
id INT(8) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(266) NOT NULL
);
create table tbl_watched (
id INT(8) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_id int(8) UNSIGNED,
movie_id int(8) UNSIGNED,
FOREIGN KEY (user_id) REFERENCES tbl_users(id),
FOREIGN KEY (movie_id) REFERENCES tbl_movies(id)
);
INSERT INTO tbl_users (name) VALUES
("John Doe"),("Jane Doe"),("Jamie Fox");
INSERT INTO tbl_movies (name) VALUES
("The Pianist"),("Django Unchained"),("Pulp Fiction"),("Wanted");
INSERT INTO tbl_watched (user_id,movie_id) VALUES
(1,1),(1,2),(1,3),(2,3),(2,4);
Sample query:
SELECT u.*
, COUNT(w.id) watched_count
FROM tbl_watched w
RIGHT
JOIN tbl_users u
ON u.id = w.user_id
GROUP
BY u.id;
On Tinder, when 2 members like each other, they are a "match" and are able to communicate. If only one member likes another, then it's not a match.
I'm trying to store this "Like" system in MySQL but can't figure out the best way to do it that's efficient. This is my setup right now.
mysql> desc likes_likes;
+--------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| from_user_id | int(11) | NO | MUL | NULL | |
| to_user_id | int(11) | NO | MUL | NULL | |
| value | int(11) | NO | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | YES | | NULL | |
+--------------+----------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
To find my matches, I would query something like...
SELECT to_user_id FROM likes_likes WHERE from_user_id = my_id AND value = 1 AND .... I don't know how to join the same table from here.
How do I perform the query on this table? If it's not efficient, what's a better structure to store this model?
1 is like, 0 is not like. Those are the only 2 values.
SELECT A.from_user_id AS userA, B.from_user_id AS userB
FROM likes_likes A
JOIN likes_likes B
ON A.from_user_id = B.to_user_id
AND A.to_user_id = B.from_user_id
AND A.id <> B.id
WHERE A.value = 1
AND B.value = 1
To find matches you can use a regular join with alias:
SELECT l1.from_user_id user1, l2.from_user_id user2
FROM likes_likes l1
INNER JOIN likes_likes l2 ON
l2.from_user_id = l1.to_user_id AND
l1.to_user_id = l2.from_user_id AND
l1.value = 1 AND l2.value = 1
The first condition checks whether the person user1 has liked or not liked user2 and that user2 has liked at least one other person.
The second condition completes the check so that we now have two persons who have expressed an opinion about each other.
The last two checks make sure that they both like each other :)
Here's a way using group by least(),greatest() to get each unique pair of users into a group and then checking if there are 2 rows per group
select least(from_user_id,to_user_id), greatest(from_user_id,to_user_id)
from likes_likes
where value = 1
-- and my_id in (from_user_id,to_user_id)
group by least(from_user_id,to_user_id), greatest(from_user_id,to_user_id)
having count(*) = 2
If it's possible to have multiple likes from the same user to another user (i.e. user 'A' likes user 'B' twice) then use having count(distinct from_user_id) = 2
Do you actually need value? If there is no row there is no like. From this query you should get 1 for a match and 0 for no mutual match.
SELECT
COUNT(*)
FROM
likes_like i_like_you
JOIN likes_like you_like_me ON i_like_you.to_user_id = you_like_me.from_user_id
WHERE
i_like_you.from_user_id = #my_id
AND you_like_me.from_user_id = #your_id
Is there any reason for id? It seems like the pair (from_user_id, to_user_id) should be UNIQUE, hence could be the 'natural' PRIMARY KEY.
I have yet to see any good argument for needing value.
So the table has shrunk to
CREATE TABLE likes_likes (
from_user_id ...,
to_user_id ...,
created_at ...,
updated_at ...,
PRIMARY KEY(from_user_id, to_user_id), -- serves as the necessary INDEX.
) ENGINE=InnoDB;
SELECT A.from_user_id AS userA,
B.from_user_id AS userB
FROM likes_likes A
JOIN likes_likes B
ON A.from_user_id = B.to_user_id
AND A.to_user_id = B.from_user_id
(I'm assuming you disallow a person liking himself.)
Trades
id |Trade_name |
1 | trade1 |
2 | trade2 |
3 | trade3 |
4 | trade4 |
Users
Name | Primary_id(FK to trade.id) | secondary_id (FK to trade.id) |
John | 1 | 2 |
Alex | 3 | 4 |
This is my current SQL which joins trades.t1 to primary & secondary.id:
select
`users`.`name` ,
`t1`.`trade_name` AS `Primary_trade`,
`t2`.`trade_name` AS `Secondary_trade`,
FROM `users`
right JOIN `trades` `t1` On (`t1`.`trade_id` = `users`.`primary_trade_id`)
right JOIN `trades` `t2` on (`t2`.`trade_id` = `users`.`secondary_trade_id`)
My question is, how do I identify which trades are not used for users both as primary or secondary. I want to see record where a trade does not exist in both primary or secondary column so I can perform housekeeping.
Thanking you all in advance for your help.
If you need only the trades rows
SELECT t.*
FROM trades t
WHERE NOT EXISTS ( SELECT 'u'
FROM Users u
WHERE u.Primary_id = t.id
OR u.Secondary_id = t.id
)
I think this should work for you:
SELECT * FROM trades WHERE id NOT IN (SELECT Primary_id FROM Users) AND id NOT IN (SELECT Secondary_id FROM Users)
It selects the rows which are not in either primary_id nor secondary_id