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
Related
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
SELECT users.firstname,
users.lastname,
users.username,
users.image_url,
posts.status,
posts.created_at as created_at,
posts.updated_at as updated_at,
share.post_user_id as user_id,
posts.id,
share.message as message,
share.user_id as share_user_id
FROM `share` INNER JOIN `posts` ON share.post_id = posts.id
INNER JOIN `users` ON users.id = share.user_id
So, as you can see here, I'm getting the firstname, lastname, and username from the table users based on the users.id = share.user_id. The share table is like this:
Shares
message
user_id
post_user_id
post_id
Now how can I get the set of names based on users.id = share.post_user_id?
You just need another inner join on table users using an alias eg user2
SELECT users.firstname,
users.lastname,
users.username,
users.image_url,
posts.status,
posts.created_at as created_at,
posts.updated_at as updated_at,
share.post_user_id as user_id,
posts.id,
share.message as message,
share.user_id as share_user_id,
users2.firstname,
users2.lastname,
users2.username,
FROM `share`
INNER JOIN `posts` ON share.post_id = posts.id
INNER JOIN `users` ON users.id = share.user_id
INNER JOIN `users` AS user2 ON users2.id = post_user_id.user_id
I have 2 tables:
posts: userid, lastuserid
users: id, name
I need to join posts.userid = users.id and posts.lastuserid = users.id to get username and lastusername.
My query did as below:
SELECT posts. * , users.name, vUsers.name
FROM posts
INNER JOIN users ON users.id = posts.userid
INNER JOIN Users ON vUsers.id = posts.lastuserid
Is there any other (better) way to do this?
Your query is probably correct. I would encourage you to use table aliases that are abbreviations for the things you are looking for:
SELECT p. * , u.name as username, l.name as lastusername
FROM posts p INNER JOIN
users u
ON u.id = p.userid INNER JOIN
users l
ON l.id = p.lastuserid;
Your query has something called vUsers, which is not defined.
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 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.