The original query is
SELECT user_id, user_location, displayname FROM engine4_users WHERE user_location IS NOT NULL
Which prints the user_id, user_location and displayname,
Ex:
1 30,32 demo
2 30,31 demo
3 33,32 demo
but now i need ot get the user picture from another table and i have this,
SELECT s.storage_path as url FROM engine4_storage_files as s
LEFT JOIN engine4_users as u ON s.file_id = u.photo_id
Which prints the URL only
What I need is to add that url to the first query
Try this:
SELECT u.user_id, u.user_location, u.displayname, s.storage_path as url
FROM engine4_users u
LEFT JOIN engine4_storage_files s
ON s.file_id = u.photo_id
WHERE u.user_location IS NOT NULL
Extended by request in comments:
SELECT u.user_id, u.user_location, u.displayname, w.value as about, s.storage_path as url
FROM engine4_users u
LEFT JOIN engine4_user_fields_values w
ON w.item_id = u.user_id
LEFT JOIN engine4_storage_files s
ON s.file_id = u.photo_id
WHERE u.user_location IS NOT NULL
Related
I have MySql query like this:
SELECT name, id_number, hr.id, file, created_at, updated_at
From users u
LEFT JOIN homework_targets ht on u.class_id = ht.class_id
LEFT JOIN homework_replies hr on u.id = hr.user_id
WHERE u.role = 'student' AND hr.homework_id = 8
This query work just fine, but not like what I expected. I want to display all the student (users) record is displayed although they don't have a record on homework_replies that match the homework_targets is it possible? if it possible how can I accomplish it? thanks.
Move the hr.homework_id condition from WHERE to ON to get true LEFT JOIN result:
SELECT name, id_number, hr.id, file, created_at, updated_at
From users u
LEFT JOIN homework_targets ht on u.class_id = ht.class_id
LEFT JOIN homework_replies hr on u.id = hr.user_id AND hr.homework_id = 8
WHERE u.role = 'student'
(If you have it in the WHERE clause, you'll get regular INNER JOIN result.)
I have a mysql select statement as below:-
select user.username, chat.from_user,chat.to_user,chat.message,chat.date_created
from chat join user on (chat.from_user or chat.to_user) in (user.user_id)
where(chat.from_user = 3 or chat.to_user = 3) and chat.chat_id IN
(SELECT distinct (MAX(chat.chat_id) )
FROM chat GROUP BY chat_group_id);
and here is my result
I always get username = admin. My expectation result is username will get correct from / to user.
Please help. Thank you.
SELECT
IF(chat.from_user=3, to_user
, IF(chat.to_user=3, form_user, 0)) AS username,
chat.from_user,chat.to_user,chat.message,chat.date_created
FROM chat
LEFT JOIN user fr_user ON chat.from_user = user.user_id
LEFT JOIN user to_user ON chat.to_user = user.user_id -- since you only want to show the TO_USERNAME, you can remove above line
WHERE (chat.from_user = 3 OR chat.to_user = 3) and chat.chat_id
IN
(SELECT distinct (MAX(chat.chat_id) )
FROM chat GROUP BY chat_group_id);
I think you intend:
select u.username, c.from_user, c.to_user, chat.message, c.date_created
from chat c join
user u
on u.user_id in (c.from_user, c.to_user)
where 3 in (c.from_user, c.to_user) and
c.chat_id in (select max(c2.chat_id)
from chat c2
group by chat_group_id
);
Initial:
1_ = country
2_ = gender
3_ = purpose
How to make SQL query show data like this
Try this(this is a mysql approach), and next time please post your table schema with text not pictures if you want to ask a question in SO.
select
u.id,
u.name,
max(case when ud.code like '1_%' then fc.code_name else '' end) as country,
max(case when ud.code like '2_%' then fc.code_name else '' end) as gender,
max(case when ud.code like '3_%' then fc.code_name else '' end) as purpose,
group_concat(distinct vt.visit_to) as visit_to
from t_users u
left join t_user_details ud
on u.id = ud.id_user
left join t_field_code fc
on ud.code = fc.code
left join t_visit_to vt
on u.id = vt.id_user
group by u.id, u.name
This would do the job more or less:
SELECT user.id,
user.name,
country.code_name AS country,
gender.code_name AS gender,
purpose.code_name AS purpose,
visit.to_to
FROM t_users AS user
JOIN t_user_details AS details
ON user.id = details.user_id
LEFT OUTER JOIN t_field_code AS country
ON details.code = country.code
AND country.code LIKE '1_%'
LEFT OUTER JOIN t_field_code AS gender
ON details.code = gender.code
AND gender.code LIKE '2_%'
LEFT OUTER JOIN t_field_code AS purpose
ON details.code = purpose.code
AND purpose.code LIKE '3_%'
LEFT OUTER JOIN t_visit_to AS visit
ON user.id = visit.id_user
ORDER BY user.id;
The only problem is that it gives multiple rows in case the user has more places to visit.
I want to get the last item as result using max(), but i'm just getting the first item even if im using max()
Here's the SQL code:
SELECT r.correct, r.items, r.percentage,MAX(r.date_taken) as date_taken,
u.username,u.FN, u.user_course_type,
IFNULL(u.user_major_type,'N/A') as user_major_type,u.level_name,
u.section_name
FROM bcc_fs_exam_result r
INNER JOIN
(SELECT u.id_user, u.username, CONCAT(u.lastname,', ',u.firstname) as FN,
c.user_course_type, m.user_major_type, l.level_name, s.section_name
FROM bcc_fs_user u
LEFT JOIN bcc_fs_user_course c on c.id_user_course = u.id_user_course
LEFT JOIN bcc_fs_user_major m on m.id_user_major = u.id_user_major
LEFT JOIN bcc_fs_group_level l ON l.id_level = u.id_level
LEFT JOIN bcc_fs_group_section s ON s.id_section = u.id_section
) u ON r.id_user = u.id_user WHERE r.id_exam = 5 GROUP BY r.id_user
TIA
What I take from your question is that you want to get the "correct", "items", "percentage" ect. columns of the row in bcc_fs_exam_result which has the last or first date.
If that's correct then you can filter bcc_fs_exam_result by first finding what is the min or max date by for each id_user then join that back on the exam results table.
SELECT
r.correct,
r.items,
r.percentage,
r.date_taken,
u.username,
u.FN,
u.user_course_type,
IFNULL(u.user_major_type,'N/A') as user_major_type,
u.level_name,
u.section_name
FROM bcc_fs_exam_result r
INNER JOIN (
SELECT u.id_user,
u.username,
CONCAT(u.lastname,', ',u.firstname) as FN,
c.user_course_type,
m.user_major_type,
l.level_name,
s.section_name
FROM bcc_fs_user u
LEFT JOIN bcc_fs_user_course c on c.id_user_course = u.id_user_course
LEFT JOIN bcc_fs_user_major m on m.id_user_major = u.id_user_major
LEFT JOIN bcc_fs_group_level l ON l.id_level = u.id_level
LEFT JOIN bcc_fs_group_section s ON s.id_section = u.id_section
) u ON r.id_user = u.id_user
INNER JOIN (
SELECT
id_user, max(r.date_taken) as last_date_taken
FROM bcc_fs_exam_result
GROUP BY id_user
) as lastdate ON lastDate.id_user = r.id_user and r.date_taken = lastdate.last_date_taken
which could be more simply written as :
SELECT
r.correct,
r.items,
r.percentage,
r.date_taken,
u.username,
CONCAT(u.lastname,', ',u.firstname) as FN,
c.user_course_type,
IFNULL(m.user_major_type,'N/A') as user_major_type,
l.level_name,
s.section_name
FROM bcc_fs_exam_result r
INNER JOIN (
SELECT
id_user, max(r.date_taken) as last_date_taken
FROM bcc_fs_exam_result
GROUP BY id_user
) as lastdate ON lastDate.id_user = r.id_user and r.date_taken = lastdate.last_date_taken
INNER JOIN bcc_fs_user u on r.id_user = u.id_user
LEFT JOIN bcc_fs_user_course c on c.id_user_course = u.id_user_course
LEFT JOIN bcc_fs_user_major m on m.id_user_major = u.id_user_major
LEFT JOIN bcc_fs_group_level l ON l.id_level = u.id_level
LEFT JOIN bcc_fs_group_section s ON s.id_section = u.id_section
You have assumed that id_user + date_taken is a surrogate key of bcc_fs_exam_result which you should probably enforce with a constraint if at all possible. Otherwise it looks from your sample data that the order of the unique id column id_result follows the date_takenso you might be better to use Max(id_result) rather than Max(date_taken). That would avoid returning duplicate rows for one id_user where two rows of bcc_fs_exam_result have the same taken_date and `id_user.
I've a SQL Query:
SELECT r.*, t.title, t.active, ticket_author.username as ticket_author, responser.username, responser.isAdmin, responser.isMod
FROM `support_tickets_replies` r
LEFT JOIN `support_tickets` t ON (t.id = r.tid)
LEFT JOIN `users` ticket_author ON (ticket_author.id = t.uid)
LEFT JOIN `users` responser ON (responser.id = r.uid)
WHERE r.tid = [something goes here]
I must check, does that ticket belongs to current user. User ID is in t.uid. When it's not that user, just returns column "error" with message "Forbidden". It's possible to do with only MySQL?
SELECT r.*, t.title, t.active, ticket_author.username as ticket_author, responser.username, responser.isAdmin, responser.isMod
FROM `support_tickets_replies` r
LEFT JOIN `support_tickets` t ON (t.id = r.tid)
LEFT JOIN `users` ticket_author ON (ticket_author.id = t.uid)
LEFT JOIN `users` responser ON (responser.id = r.uid)
WHERE r.tid = [something goes here]
AND t.uid = [User ID goes here]
This query will only turn up records that belong to the user.
If the record doesn't belong to the user, it will return nothing.