I'm trying to create a query that checks an inserted value against a field. I'm trying to accomplish this using a sub-query, but got stuck. What I'm trying to accomplish:
(message1) User1 says: Hello User2
(message2) ChatX says: User1, user2 said hello to you!
I figured that in order to accomplish that, I need a subquery within the like statement.
SELECT chat.id, chat.userid, chat.message, user.userid, user.username
FROM chat, user
WHERE LOWER(message) LIKE CONCAT('hello ', (SELECT user.username FROM user WHERE XXX = user.username))
AND chat.userid = user.userid
The XXX in the LIKE statement, is the username someone said hello to. It's there it should check against the user table and if the message matches a user output ChatX's line. My question, how do I make XXX work and how do I set it?
SELECT c.id, c.message,
sender.userid, sender.username,
receiver.userid, receiver.username
FROM chat c
JOIN user sender ON c.userid = sender.userid
JOIN user receiver ON LOWER(message) like CONCAT('hello ', receiver.username)
Related
I need help with a complex select statement in SQL. I have these two table here:
Table user:
Table contacts_from_user:
When I make a select
SELECT name, vorname, gebdat, bezeichnung, wert
FROM user
JOIN contacts ON u_id = user_u_id
I get multiple lines for one user because he has more then one contact options but I need to put it in just one line:
The line should be looks like this:
name, vorname, gebdat, bezeichung_1, wert_1, bezeichnung_2, wert_2.......
How ca I do this?
Thanks a lot!
In pseudo-code, the best way to handle such scenarios is:
query = SELECT A.ID, A.stuff, B.stuff FROM A JOIN B ON A.ID = B.A_ID
results = run query
prev_A_ID = impossible_A_ID
for each result
if prev_A_ID not equal result_A_ID
create new A and set as current A
add B.stuff to current A
set prev_A_ID to result_A_ID
end for
I have question . If i have user table and a column name be freinds .
I want use some user id inside a column friends with this value : 1|2|45|18 .
This means for example user id =5 have 4 freinds with these id 1|2|45|18.
I don't want use two table for this situation.
How can select each users data when friends with user id = 5 ?
In php we have explode function for slice each delimeter .
Can i set delimeter for this target or i should use two table user and friends in MySQL ?
It's an awful case, especially if you inherit some DB with such a scheme, but to find friends you can use:
SELECT friend.*
FROM user u
left join user friend on concat('|', o.friends, '|') like concat('%|', friend.id, '|%')
where u.id = 5;
Say I have the following tables
User
__________
id
username
email
FriendGame
__________
id
useroneid
usertwoid
status
I want to get games that the current user is part of, so I do this:
SELECT *
FROM FriendGame
WHERE useroneid=1663702020516206
OR usertwoid=1663702020516206
AND STATUS =1;
This is fine. Now I want to join the username, but only for the user that ISNT the supplied user (1663702020516206) since in FriendGame the given user exists EITHER as useroneid or usertwoid.
You can pretty much translate your logic directly into an on clause:
SELECT fg.*
FROM FriendGame fg JOIN
User u
ON (fg.useroneid = 1663702020516206 and fg.usertwoid = u.id) or
(fg.usertwoid = 1663702020516206 and fg.useroneid = u.id)
WHERE 1663702020516206 in (fg.useroneid, fg.usertwoid) AND
STATUS = 1;
Actually, the where clause is not necessary to get the right result set, but I think it makes the intention of the query clearer.
I have following messages table:
I want to list all messages in a inbox like for example,
lets assume I am user number 1 so my inbox should show
me following:
Messages with user 2
Messages with user 3
Messages with user 4
What is the best way to build a query for this?
I've came up with somewhat weird query that looks like: (pseudo code)
SELECT
*
FROM
`message`
WHERE
(`user_id_sender` = 1 AND `user_id_receiver` NOT 1)
OR
(`user_id_sender` NOT 1 AND `user_id_receiver` = 1)
Thank you
That's very simple
SELECT user_id_sender ID,
(SELECT user_id_reciever
FROM messages
WHERE user_id_sender NOT IN(ID)
GROUP BY user_id_reciever
)
FROM message
How I can select name of creator and editor from users table, creator and editor are different ids in same table, but user table is different table
Is it what you mean?
$sql = "
SELECT id,name
FROM users
WHERE users.id = editors_table.$editor_id
OR users.id = creators_table.$creator_id";
from what i understand, are you saying you have 3 tables - 1 with creator data, 1 with editor data, and a third that references a record in each of the tables using an id?
if so, you'll have to use JOINs to achieve what you want - something like:
SELECT id, name, editors_table.editor_id, creators_table.creator_id
FROM users
LEFT JOIN editors_table ON user.editor_id = editor_table.editor_id
LEFT JOIN creators_table ON user.creator_id = creator_table.creator_id
WHERE editor_table.editor_id = $editor_id_var
OR creator_table.creator_id = $creator_id_var
(you'll want to go through the query as I'm guessing here)