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
Related
I have the following query:
SELECT u.id, u.name, u.date_registered, p.time_created
FROM users u JOIN
prospect_notes p
ON u.id=p.subject_id
WHERE u.allocated_instructors = 668
AND p.time_created = (SELECT MAX(p2.time_created) FROM prospect_notes p2 WHERE p2.subject_id = p.subject_id)
ORDER BY p.time_created;
My problem is that when there are no rows in the prospect_notes table which match the following:
AND p.time_created = (SELECT MAX(p2.time_created) FROM prospect_notes p2 WHERE p2.subject_id = p.subject_id)
I get no result.
Instead, I want all the rows in the users table to return (presumably p.time_created would be NULL in such cases).
You need to be careful because of the JOIN clause. Presumably, if there are no matches for the correlated subquery, there are no matches in the JOIN either.
So, use LEFT JOIN and move the logic to the FROM clause:
SELECT u.id, u.name, u.date_registered, p.time_created
FROM users u LEFT JOIN
prospect_notes p
ON u.id = p.subject_id LEFT JOIN
(SELECT p2.subject_id, MAX(p2.time_created) as max_time_created
FROM prospect_notes p2
GROUP BY p2.subject_id
) p2
ON p2.subject_id = p.subject_id AND p2.time_created = p.time_created
WHERE u.allocated_instructors = 668
ORDER BY p.time_created;
That said, if you only want time_created from prospect_notes, then use a simpler query:
SELECT u.id, u.name, u.date_registered, MAX(p.time_created)
FROM users u LEFT JOIN
prospect_notes p
ON u.id = p.subject_id
WHERE u.allocated_instructors = 668
GROUP BY u.id -- okay, assuming id is unique or a primary key
ORDER BY MAX(p.time_created);
You need LEFT JOIN this clause permit you to have NULL values in the right table.
Try :
SELECT u.id, u.name, u.date_registered, p.time_created
FROM users u
LEFT JOIN prospect_notes p
ON u.id=p.subject_id
WHERE u.allocated_instructors = 668
AND p.time_created = (SELECT MAX(p2.time_created) FROM prospect_notes p2 WHERE p2.subject_id = p.subject_id)
ORDER BY p.time_created;
EDIT the SQL version (mysql 5.7) I have does not support row_number() ... what alternative method can I used?
I researched an found out that using row_number() where is my query would I apply it... the columns I want to check are user_id racid and ios
SELECT
ur.user_id,
u.racid,
u.fname,
u.lname,
u.email,
u.last_login,
ur.role_id,
r.name AS role_name,
i.device_token AS ios,
dg.group_name,
dg.ad_group,
a.device_token AS android
FROM
users AS u
LEFT OUTER JOIN
user_roles AS ur ON u.id = ur.user_id
LEFT OUTER JOIN
roles AS r ON ur.role_id = r.id
LEFT OUTER JOIN
ios_tokens AS i ON u.racid = i.racf_id
LEFT OUTER JOIN
android_tokens AS a ON u.racid = a.racf_id
LEFT OUTER JOIN
dashboard_groups AS dg ON dg.role_id = r.id
Current Table after joining
user_id, racid, fname, lname, email, last_login, role_id, role_name, ios, group_name, ad_group, android
You can try below it will work for mysql 8+
select * from
(SELECT
ur.user_id,
u.racid,
u.fname,
u.lname,
u.email,
u.last_login,
ur.role_id,
r.name AS role_name,
i.device_token AS ios,
dg.group_name,
dg.ad_group,
a.device_token AS android,row_number() over(partition by ur.user_id,u.racid,i.device_token order by ur.user_id,
u.racid,i.device_token) as rn
FROM
users AS u
LEFT OUTER JOIN
user_roles AS ur ON u.id = ur.user_id
LEFT OUTER JOIN
roles AS r ON ur.role_id = r.id
LEFT OUTER JOIN
ios_tokens AS i ON u.racid = i.racf_id
LEFT OUTER JOIN
android_tokens AS a ON u.racid = a.racf_id
LEFT OUTER JOIN
dashboard_groups AS dg ON dg.role_id = r.id
)X where rn<>1
OR Mysql below 8+ version you can try following
select * from
( SELECT
ur.user_id,
u.racid,
u.fname,
u.lname,
u.email,
u.last_login,
ur.role_id,
r.name AS role_name,
i.device_token AS ios,
dg.group_name,
dg.ad_group,
a.device_token AS android,
#row_number:=CASE
WHEN #user_id = ur.user_id and #racid = u.racid and #evicetoken = i.device_token THEN #row_number + 1
ELSE 1
END AS num,
#user_id :=ur.user_id, #racid = u.racid, #evicetoken = i.device_token
FROM
users AS u
LEFT OUTER JOIN
user_roles AS ur ON u.id = ur.user_id
LEFT OUTER JOIN
roles AS r ON ur.role_id = r.id
LEFT OUTER JOIN
ios_tokens AS i ON u.racid = i.racf_id
LEFT OUTER JOIN
android_tokens AS a ON u.racid = a.racf_id
LEFT OUTER JOIN
dashboard_groups AS dg ON dg.role_id = r.id,
(SELECT #user_id:=0,#racid=0,#evicetoken=0,#row_number:=0) as t
)X where num<>1
This is not about removing duplicate rows but if you need uniqueness from single table for multiple columns you can use unique index with multiple columns. If this is not you need kindly ignore this.
ALTER TABLE `users` ADD UNIQUE `unique_index`(`col1`, `col2`, `col3`);
How do I specify unique constraint for multiple columns in MySQL?
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 the following SQL query:
SELECT users.user_id,
users.first_name,
users.last_name,
roles.role,
roles.role_id,
users.username,
users.description,
users_vs_teams.team_id,
teams.team_name,
teams.status,
teams.notes
FROM teams
INNER JOIN users_vs_teams ON teams.team_id = users_vs_teams.team_id
RIGHT OUTER JOIN users ON users_vs_teams.user_id = users.user_id
INNER JOIN roles ON users.role_id = roles.role_id
WHERE( users.role_id = 3 ) AND ( teams.status = 'Completed' ) OR ( teams.status IS NULL )
I want to display only users with a role_id of 3 but team.status can be either Completed or NULL. However, this query displays all roles where teams.status is either Completed or NULL. Any help resolving this issue will be greatly appreciated.
First, I'm not sure if you need an outer join for this. Second, your problem seems to be parentheses in the WHERE clause:
SELECT u.user_id, u.first_name, u.last_name, r.role, r.role_id,
u.username, u.description, uvt.team_id,
t.team_name, t.status, t.notes
FROM teams t INNER JOIN
users_vs_teams uvt
ON t.team_id = uvt.team_id INNER JOIN
users u
ON uvt.user_id = u.user_id
roles r
ON u.role_id = r.role_id ON u
WHERE (u.role_id = 3) AND (t.status = 'Completed' OR t.status IS NULL)
Note that table aliases make the query easier to write and to read.
Remove the RIGHT OUTER JOIN and fix your parenthesis in your WHERE clause.
SELECT users.user_id,
users.first_name,
users.last_name,
roles.role,
roles.role_id,
users.username,
users.description,
users_vs_teams.team_id,
teams.team_name,
teams.status,
teams.notes
FROM teams
INNER JOIN users_vs_teams ON teams.team_id = users_vs_teams.team_id
INNER JOIN users ON users_vs_teams.user_id = users.user_id
INNER JOIN roles ON users.role_id = roles.role_id
WHERE( users.role_id = 3 ) AND ( teams.status = 'Completed' OR teams.status IS NULL)
you can also do something like this:
( teams.status = 'Completed' OR ISNULL(teams.status,'') = '')
I have this query to get information from two tables:
SELECT u.username, u.id,
SUM(t.result = 1) AS winnings,
SUM(t.result = 2) AS loses
FROM users u
LEFT JOIN tahminler t ON u.id = t.user_id
GROUP BY u.id
I want to get comments_no for each user from another table; something like this:
SELECT u.username, u.id,
SUM(t.result = 1) AS winnings,
SUM(t.result = 2) AS loses,
f1.comments_no
FROM users u
LEFT JOIN tahminler t ON u.id = t.user_id
INNER JOIN (select count(distinct match_static_id) as comments_no,user_id from comments where user_id = "here is my problem")
GROUP BY u.id
Is it possible to but the value of u.id in the where user_id = u.id.
Briefly How to get the comments_no for each user in my query.?
Use a GROUP BY in the sub query:-
SELECT u.username, u.id,
SUM(t.result = 1) AS winnings,
SUM(t.result = 2) AS loses,
f1.comments_no
FROM users u
LEFT JOIN tahminler t ON u.id = t.user_id
INNER JOIN (select user_id, count(distinct match_static_id) as comments_no from comments GROUP BY user_id) f1
ON u.id = f1.user_id
GROUP BY u.id