I have a simple chat database like this:
id | from | to | message
---|------|----|-----------------
1 | 1 | 2 | hello
2 | 1 | 2 | are you there?
3 | 2 | 1 | yes I'm here!
Also there is a second table that holds the names
`users`.`firstname` and `users`.`lastname`
I'm trying to combine the two, so when retrieving the chats, I'd also have the names of both participants. My query looks like this right now:
SELECT
`messages`.*,
CONCAT(`users`.`firstname`, " ", `users`.`lastname`) as `nameFrom`
FROM `messages`
INNER JOIN
`users` ON `messages`.`from` = `users`.`id`
It works fine, but if I try to add another join, just change messages.from to messages.to I get errors.
How do I combine the first name & last name of each participant when retrieving the chat log?
Thanks
Looks like you're trying to get both the sender and receiver? If so, this should work joining the Users table twice:
SELECT
`messages`.*,
CONCAT(u.`firstname`, " ", u.`lastname`) as `nameFrom`,
CONCAT(u2.`firstname`, " ", u2.`lastname`) as `nameTo`
FROM `messages`
INNER JOIN
`users` u ON `messages`.`from` = u.`id`
INNER JOIN
`users` u2 ON `messages`.`to` = u2.`id`
You need to basically join the table users twice on table messages provided that you supply an ALIAS to uniquely identify the tables with the same table name.
SELECT messages.*,
CONCAT(a.firstname, ' ', a.lastname) as nameFrom,
CONCAT(b.firstname, ' ', b.lastname) as nameTo
FROM messages
INNER JOIN users a
ON messages.from = a.id
INNER JOIN users b
ON messages.to = b.id
SQLFiddle Demo
Related
I have this table:
users
id | name
---------------------
1 | Lisa
2 | John
and i have this users list:
(1,2,2,2,1,2)
and i want to get this result:
Lisa,John,John,John,Lisa,John
how i can get this result just using SQL query in my Phpmyadmin without using PHP?
i must use Left Join ?
(Assuming orders table is on the same server) You should use inner join here. Because, there should be an user to have an entry in orders table.
SELECT abc = STUFF(( Select user.user_name + ',' from
orders inner join users on users.id = orders.user_id where orders.user_id in (your string of user ids) for
xml_path(' ')), 1, 1, '') FROM temp1
I have some data like this
for user
id | username | email
1 | test | test#test.com
2 | om | test2#test2.com
3 | aa | test3#test3.com
And I have data user biodata like this
id | username | bio
1 | test | test
2 | om | test2
So, I want use not in with inner join for showing data with where user.username not in biodata.username and I try like this but it's error
select user.*, biodata.* from user inner join biodata on user.username = biodata.username where user.username not in biodata.username;
So, how to use that?
This calls for the common LEFT JOIN ... IS NULL pattern.
SELECT u.id, u.username, u.email
FROM user u
LEFT JOIN biodata b ON u.username = b.username
WHERE b.id IS NULL
The LEFT JOIN operation preserves all rows in user, whether or not they have matches in biodata. (By contrast, an ordinary JOIN would suppress rows from user that didn't have matches.) The b.id IS NULL operation filters out the rows that do have matches.
She wants the names (user.username) that are not present in biodata table. Left Join preserves ALL records from table users and MATCHING records fom table biodata (good explanation in W3Schools Left Join). The where clause searches usernames that are not present in biodata:
SELECT u.id, u.username, u.email
FROM user u
LEFT JOIN biodata bd
ON u.username = bd.username
WHERE u.username
NOT IN (select username from biodata);
The correct output is:
id | username | email
3 | aa | test3#test3.com
I belive that you are trying to do an 'EXCEPT'. But some SGBDS like MySQL don't have this option.
Hello i am stuck with this problem i have tables
Complaints:
Complaint table
User:
User table
and i want to extract complaints of the users that are in the specific province
example: if i want to extract the complaints of the province of ILOCOS my table would look like this
complaint_id | user_id | complaint_title| complaint_category | complaint_desc
---------------------------------------------------------------------------------
17 | 2 | 2 | Accidents | qwe123
ive arrived with this sql statement :
SELECT DISTINCT complaint.complaint_id, complaint.user_id,
complaint.complaint_title,complaint.complaint_desc
FROM complaint LEFT JOIN
user
ON user.province = 'ILOCOS' LEFT JOIN
complaint_media
ON complaint.complaint_id = complaint_media.complaint_id
You need to join complaint table with user table on user_id column otherwise it will be a cartesian join.
select distinct complaint.complaint_id, -- check if you really need "DISTINCT"
complaint.user_id,
complaint.complaint_title,
complaint.complaint_desc,
. . .
from complaint
join user
on complaint.user_id = user.user_id -- here
and user.province = 'ILOCOS'
join complaint_media
on complaint.complaint_id = complaint_media.complaint_id
Table join:
|ID|admin|user |data|
|1 |00001|00002|XXXX|
admin(fk) = users.id,
user(fk) = users.id.
Table users:
|id |name|pass|type |
|00001|root|1234|admin|
|00002|user|1235|user |
select join.*,users.name as admin,users.name as user from join
left join users on users.id=join.admin
left join users on users.id=join.user
where grrrrrrr
How can I do this?
Original query, I'm trying to run:
SELECT
visits.id,
visits.patient AS patient_id,
visits.doctor AS doctor_id,
visits.date,
visits.time_booked,
visits.time_arrived,
visits.time_start,
visits.time_end,
visits.type_id,
visits.complain,
visits.diagnosis,
visits.note,
visits.stats,
(personal.name WHERE personal.id=visits.patient and personal.role='patient') AS pt_name,
(personal.name WHERE personal.id=visits.doctor and personal.role='doctor') AS dr_name
FROM
visits ,
personal
You have to alias the table users with different aliases. Something like
select
a.*,
u1.name as admin,
u2.name as user
from `join` a
left join users u1 on u1.id = a.admin
left join users u2 on u2.id = a.`user`;
Also you have to escape the table name join, user since they are reserved keywords in MySQL. Try to avoid those names as object names.
SQL Fiddle Demo
This will give you:
| ID | ADMIN | USER | DATA |
----------------------------
| 1 | 1 | 2 | XXXX |
Update
For your query after you updated your question, you have to do this the same way, like this:
SELECT
v.id,
v.patient AS patient_id,
v.doctor AS doctor_id,
v.date,
v.time_booked,
v.time_arrived,
v.time_start,
v.time_end,
v.type_id,
v.complain,
v.diagnosis,
v.note,
v.stats,
pationts.name AS pt_name,
doctors.name AS dr_name
FROM visits v
LEFT JOIN personal pationts ON pationts.id = v.patient
AND pationts.role ='patient'
LEFT JOIN personal doctors ON doctors.id = v.patient
AND doctors.role ='doctor';
Updated SQL Fiddle Demo
I have following two tables 'USERS' and 'GROUPS':
USERS
-id
-name
-groupid
GROUP
-id
-name
I'd like to return all users along with their group's name and group id. It should be an outer join on group id field correct?
A simple INNER JOIN should be enough:
SELECT `USERS`.*, `GROUP`.name AS group_name
FROM `USERS`, `GROUP`
WHERE `USERS`.groupid = `GROUP`.id
You're going to want to look at the JOIN statement
Doing this from my phone, so pardon any moderately incorrect syntax, but something a long the lines of
Edit: other guy's syntax is better. It's too early here
You can use a LEFT JOIN between users and groups so that users who are not in a group still show up in the result set, but with group name and id NULL:
SELECT
a.*,
b.name AS group_name
FROM
users a
LEFT JOIN
`group` b ON a.group_id = b.id
Side note: Ensure that you're encasing the table name group in backticks because it is a reserved keyword.
The result-set should look something like:
id | name | group_id | group_name
-----------------------------------------------------------------------------
1 | John | 5 | ThisIsGroup5
3 | Tim | 3 | ThisIsGroup3
6 | NotInGroup | NULL | NULL
Changing LEFT to INNER in the above query would INNER JOIN the two tables and exclude the user "NotInGroup" from the result-set.