I have the following query and want to order the results by the CREATED column, how would I go about doing it?
(SELECT media.id, media.accessKey, media.internalName, media.type, media.modified, users.username, users.id, media.created,
0 AS reposted
FROM media
LEFT JOIN users ON users.id = media.userId)
UNION
(SELECT media.id, media.accessKey, media.internalName, media.type, media.modified, users.username, reposts.userId, reposts.created,
1 AS reposted
FROM reposts
LEFT JOIN media ON media.id = reposts.mediaId
LEFT JOIN users ON users.id = reposts.userId)
You just need to add the order by clause to your query (the use of ( around the queries is not mandatory):
SELECT media.id,
media.accessKey,
media.internalName,
media.type,
media.modified,
users.username,
users.id,
media.created,
0 AS reposted
FROM media
LEFT JOIN users ON users.id = media.userId
UNION
SELECT media.id,
media.accessKey,
media.internalName,
media.type,
media.modified,
users.username,
reposts.userId,
reposts.created,
1 AS reposted
FROM reposts
LEFT JOIN media ON media.id = reposts.mediaId
LEFT JOIN users ON users.id = reposts.userId
ORDER BY CREATED ASC
You can check this sqlfiddle to see it working.
Add:
order by created
At the end of your query.
Related
I have the following SQL query which returns 2 results which are the same only different by a where clause.
I would like to have them combined and group them under the user names. At the moment the two results are displayed but the results are not fully combined and the users are shown twice because of the two select statements.
How can I combine the two to create a single result.
The SQL query is as follows
SELECT
COUNT (asings.user_id),
post_status.status_id as status_id,
users.name,
users.id as user_id
from asigns
LEFT JOIN
post_status on asigns.post_id = post_status.post_id
RIGHT JOIN
users on asigns.user_id = users.id
WHERE
post_status.status_id = 2
GROUP BY users.id
UNION
SELECT
COUNT(asigns.user_id),
post_status.status_id as status_id,
users.name,
users.id from asigns
LEFT JOIN
post_status on asigns.post_id = post_status.post_id
RIGHT JOIN
users on asigns.user_id = users.id
WHERE
post_status.status_id = 3
GROUP BY users.id
See in this photo the usernames are appearing twice, what I want is to join the two username, and move count as two different columns depending on the status_id
How can I solve this. Thanks
You should use a single query with conditional aggregation:
SELECT
SUM(ps.status_id = 2) AS cnt_2,
SUM(ps.status_id = 3) AS cnt_3,
u.name,
u.id as user_id
FROM users u
LEFT JOIN assigns a ON a.user_id = u.id
LEFT JOIN post_status ps ON a.post_id = ps.post_id
GROUP BY u.id;
Use SELECT distinct for the union:
SELECT distinct * FROM ([FIRST SELECT] UNION [SECOND SELECT])
Or GROUP BY username
SELECT * FROM ([FIRST SELECT] UNION [SECOND SELECT]) GROUP BY username
BUT I suggest to review your both selects, if it differs just in the where clause so use where clause with the operator OR and you handle it just by one SELECT
Although your are using a LEFT and a RIGHT join and implicitly an INNER join between users and asigns, the WHERE clause transforms all the joins to INNER joins, because you are picking only the matching rows from post_status.
So, you only need INNER joins and conditional aggregation:
SELECT COALESCE(SUM(ps.status_id = 2), 0) counter_2,
COALESCE(SUM(ps.status_id = 3), 0) counter_3,
u.name,
u.id AS user_id
FROM users u
INNER JOIN asigns a ON a.user_id = u.id
INNER JOIN post_status ps ON ps.post_id = a.post_id
WHERE ps.status_id IN (2, 3)
GROUP BY u.id;
Sorted!!!
I have 2 tables, One table have person in charge for subscription. Second table have product users for each subscription and their details. I union both tables in mysql and query working 100% fine but when i try to filter records using where condition it return all the records without filtering.
Below you can find my query!
SELECT subscription_products.subscription_id, users.id, users.full_name,
users.company, users.job, users.birthday, users.gender,
users.nric, users.passport_number, users.phone_country_code,
users.phone_number, users.handphone_country_code, users.handphone_number,
users.email, users.nationality, wallets.current_amount,
users.created_at, users.updated_at
FROM subscription_product_users
LEFT JOIN subscription_products
ON subscription_product_users.subscription_product_id = subscription_products.id
LEFT JOIN users
ON subscription_product_users.user_id = users.id
LEFT JOIN wallets
ON users.id = wallets.user_id
UNION
SELECT person_in_charge.subscription_id, person_in_charge.user_id,
users.full_name,
users.company, users.job, users.birthday, users.gender,
users.nric, users.passport_number, users.phone_country_code,
users.phone_number, users.handphone_country_code,
users.handphone_number, users.email, users.nationality, wallets.current_amount,
users.created_at, users.updated_at
FROM person_in_charge
LEFT JOIN users
ON person_in_charge.user_id = users.id
LEFT JOIN wallets
ON person_in_charge.user_id = wallets.user_id
where subscription_id = '1378'
Can someone helps me?
Try to wrap it and it work
SELECT * FROM
(SELECT subscription_products.subscription_id, users.id, users.full_name,
users.company, users.job, users.birthday, users.gender,
users.nric, users.passport_number, users.phone_country_code,
users.phone_number, users.handphone_country_code, users.handphone_number,
users.email, users.nationality, wallets.current_amount,
users.created_at, users.updated_at
FROM subscription_product_users
LEFT JOIN subscription_products
ON subscription_product_users.subscription_product_id = subscription_products.id
LEFT JOIN users
ON subscription_product_users.user_id = users.id
LEFT JOIN wallets
ON users.id = wallets.user_id
UNION
SELECT person_in_charge.subscription_id, person_in_charge.user_id,
users.full_name,
users.company, users.job, users.birthday, users.gender,
users.nric, users.passport_number, users.phone_country_code,
users.phone_number, users.handphone_country_code,
users.handphone_number, users.email, users.nationality, wallets.current_amount,
users.created_at, users.updated_at
FROM person_in_charge
LEFT JOIN users
ON person_in_charge.user_id = users.id
LEFT JOIN wallets
ON person_in_charge.user_id = wallets.user_id)
as tempTable
where subscription_id = '1378'
Add the filter in both the queries, also I have used UNION ALL instead of UNION to have better performance. If your query will return duplicates and you want to avoid it, then replace it with UNION
You need to start using Alias in queries like this.
SELECT sp.subscription_id,
u.id,
u.full_name,
u.company,
u.job,
u.birthday,
u.gender,
u.nric,
u.passport_number,
u.phone_country_code,
u.phone_number,
u.handphone_country_code,
u.handphone_number,
u.email,
u.nationality,
w.current_amount,
u.created_at,
u.updated_at
FROM subscription_product_users spu
INNER JOIN subscription_products sp
ON spu.subscription_product_id = sp.id
LEFT JOIN users u
ON spu.user_id = u.id
LEFT JOIN wallets w
ON u.id = w.user_id
WHERE sp.subscription_id = '1378'
UNION ALL
SELECT pic.subscription_id,
pic.user_id,
u.full_name,
u.company,
u.job,
u.birthday,
u.gender,
u.nric,
u.passport_number,
u.phone_country_code,
u.phone_number,
u.handphone_country_code,
u.handphone_number,
u.email,
u.nationality,
w.current_amount,
u.created_at,
u.updated_at
FROM person_in_charge pic
LEFT JOIN users u
ON pic.user_id = u.id
LEFT JOIN wallets w
ON pic.user_id = w.user_id
WHERE pic.subscription_id = '1378'
select * from
(
(select 1 as 'a')
union
(select 2 as 'a')
) as u
where
u.a=2
Alright so for some reason I cant join 3 tables properly in with PHP and MySQL. My query worked with 2 but with 3 it wont.
select users.username,
users.ID,
users.currentTime,
users.gender,
user_ranks.likes as likes,
user_ranks.disslikes as diss,
profiles.img_url as URL
from users
inner join profiles,
user_ranks on users.ID = profiles.userID LIMIT 1
You need to join both tables with an ON clause:
select users.username,
users.ID,
users.currentTime,
users.gender,
user_ranks.likes as likes,
user_ranks.disslikes as diss,
profiles.img_url as URL
from users
inner join profiles on users.ID = profiles.userID
inner join user_ranks on user_ranks.ID = profiles.userID
LIMIT 1
SELECT
users.username,
users.ID,
users.currentTime,
users.gender,
user_ranks.likes as likes,
user_ranks.disslikes as diss,
profiles.img_url as URL
FROM users
INNER JOIN profiles ON users.ID = profiles.userID
INNER JOIN tbl_3 ON users.col = tbl_3.fk
LIMIT 1
You need to do a join for each of the tables. for Example:
select users.username,
users.ID,
users.currentTime,
users.gender,
user_ranks.likes as likes,
user_ranks.disslikes as diss,
profiles.img_url as URL from users
inner join profiles on users.ID = profiles.userID
inner join user_ranks on users.ID= user_ranks.ID LIMIT 1
SELECT users.username,
users.ID,
users.currentTime,
users.gender,
user_ranks.likes as likes,
user_ranks.disslikes as diss,
profiles.img_url as URL
FROM users
INNER JOIN profiles
on users.ID = profiles.userID
INNER JOIN user_ranks
on user_ranks.ID = profiles.userID
LIMIT 1
I have to tables: USERS and DATA. Every user from USERS can have one, multiple rows on DATA but also any.
I would like to select also the users from USERS without correspondence on DATA but the following query omit them. How should I modify it?
SELECT USERS.id,
USERS.email,
USERS.active,
USERS.last_alert,
DATA.active,
DATA.active_from
FROM USERS
JOIN DATA
WHERE DATA.id_user = USERS.id
AND DATA.active = 1
ORDER BY USERS.id ASC
DATA.id_user contains the USERS.id.
You should use LEFT JOIN instead of INNER JOIN
SELECT
USERS.id,
USERS.email,
USERS.active,
USERS.last_alert,
DATA.active,
DATA.active_from
FROM USERS
LEFT JOIN DATA ON (
DATA.id_user = USERS.id AND DATA.active = 1
)
ORDER BY USERS.id ASC
Do Outer Join
SELECT USERS.id,
USERS.email,
USERS.active,
USERS.last_alert,
DATA.active,
DATA.active_from
FROM DATA
LEFT OUTER JOIN USERS
WHERE DATA.id_user = USERS.id
AND DATA.active = 1
ORDER BY USERS.id ASC
Rewrite it as a LEFT JOIN should include those rows, but not available values will become NULL then
SELECT USERS.id,
USERS.email,
USERS.active,
USERS.last_alert,
DATA.active,
DATA.active_from
FROM USERS
LEFT JOIN DATA ON USERS.id = DATA.id_user AND DATA.active = 1
ORDER BY USERS.id ASC
I have following tables
questions -> id, question_data, user_id
users -> id, fname, lname
question_connect-> id, question_id, user_id
My initial query was as follows
select questions.id, questions.question_data, users.id, users.fname from questions, users where questions.user_id = users.id limit 30
But over here, i want count of users on that question, so i tried following query
select questions.id, questions.question_data, users.id, users.fname, count(questions_connect.id) from questions, users LEFT JOIN questions_connect ON `questions`.`id` = `questions_connect`.`question_id` where questions.user_id = users.id group by `questions_connect`.`id` limit 30
this shows error
Unknown column 'questions.id' in 'on clause'
SO can we make 1 call with natural join and left join and if yes, where i am going wrong..?
Using an explicit join should sort you out:
select questions.id, questions.question_data, users.id, users.fname, count(questions_connect.id)
from questions
join users on questions.user_id = users.id
left join questions_connect on `questions`.`id` = `questions_connect`.`question_id`
group by `questions_connect`.`id`
limit 30
You're better off specifying all your joins explicitly, try to forget that implicit joins exist.
I believe you don't need to put the quotes on the joins
ON questions.id = questions_connect.question_id