I am using SQL for the first time and can't seem to figure out how can I convert this query into a join. I want to do so, because I read this:
Join vs. sub-query
SELECT `bookings`.* FROM `bookings` WHERE `bookings`.`user_id` IN
(SELECT `users`.`id` FROM `users` WHERE `users`.`phone` = 9999999999)
I want to find only those bookings whose users belong in the user table with a given phone number.
I tried using a join, but I don't understand what the possible condition of join should be.
Thanks a lot!
You need something like that:
SELECT b.* FROM `bookings` b
INNER JOIN `users` u
ON b.user_id = u.id
WHERE u.phone = 8860990440
SELECT `bookings`.* FROM `bookings` INNER JOIN `users` ON `users`.`id` = `bookings`.`user_id` WHERE `users`.`phone` = 9999999999
Related
I have this logic query that doesn't work because I need to Join like 4 different tables, which is insane but please help me out, I will find a better way in the future.
select * from approvals
where approvable_type =
(select model from permission_configs
where variable = (
select permission_slug from role_permission
where role_id = (
select role_id from role_user
where user_id = approvals.user_id) ) )
Please give me the above query in Eloquent with multiple join! I will forever be grateful to you.
You could refactor your query using inner join instead of where and subquery
select *
from approvals a
INNER JOIN permission_configs p ON a.approvable_type = p.model
INNER JOIN role_permission r ON p.variable = r.permission_slug
INNER JOIN role_user u ON r.role_id = u.role_id
WHERE u.user_id = a.user_id
once done, you could take a look at Eloquent for join
I am working with a MySQL database. I am suppose to combine three select queries, to "improve performance". Each select below is dependent on the previous ID retrieved.
So far, I've tried the following...
# multiple select from tables
select user.name, group.ID
from user as u, group as g
where u.name = <name_here>
# inner join...
select user.ID, group.ID,
from user
inner join group
on user.ID = group.ID
I need to select the user.name and group.ID based on a username param. Is there a way to query this data in a single statement?
I don't know if I understand your need, lets try:
Try to use this query:
select pGroupMatch.GroupID, ProfileData.ID
from pUserMatch
inner join pGroupMatch on pGroupMatch.GroupID = pUserMatch.GroupID
inner join ProfileData on ProfileData.id = pGroupMatch.ProfileID
where pUserMatch.username = "<username>";
Check if you can create indexes for improve your query, if you can try it:
CREATE INDEX idx_pUserMatch_01 ON pUserMatch (GroupID);
CREATE INDEX idx_pGroupMatch_01 ON pGroupMatch (ProfileID);
Please use join for your requirement. Please try below query
select t3.* from Profiles.pUserMatch t1
left join Profiles.pGroupMatch t2 ON t2.GroupID=t1.GroupID
left join Profiles.ProfileData t3 ON t3.ID=t2.ProfileID
where t1.username = "<username>";
I hope above query will help you.Please feel free to comment. Thanks.
This is the query you get by joining the tree queries you already have:
SELECT pd.*
FROM Profiles.ProfileData pd
# ... where ID = "<profile_id>", profile_id = select ProfileID from ...
INNER JOIN Profiles.pGroupMatch pm ON pd.ID = pm.ProfileID
# ... where GroupID = "<group_id>", group_id = select GroupID from ...
INNER JOIN Profiles.pUserMatch pu ON pm.GroupID = pm.GroupID
WHERE pm.username = "<username>"
I put in comments the fragments of your queries that gets converted to JOIN subclauses.
Read more about the syntax of the JOIN subclause of the SELECT statement.
You don't need foreign keys to join stuff:
select p.* from Profiles.pUserMatch u
join Profile.pGroupMatch g on u.GroupID = g.GroupID
join Profile.ProfileData p on g.ProfileID = p.ID
where u.username = ?
I just used this code, and working properly with me:
SELECT
* FROM `order` as o
LEFT JOIN `services` as s ON s.`id` = o.`service_id`
LEFT JOIN `users` as u ON u.`id` = o.`users_id`
LEFT JOIN `files` as f ON f.`order_id` = o.`id`
but when I try to choose some fields from the 1st table, the results not showing the other tables
SELECT
o.`id` AS `id`,
o.`service_id`,
o.`extras`,
o.`quantity`,
o.`price`,
o.`links`,
o.`keywords`,
o.`status_id`,
o.`users_id`,
o.`date`,
o.`notes`,
o.`c_reason`,
o.`agent_star`
FROM `order` as o
LEFT JOIN `services` as s ON s.`id` = o.`service_id`
LEFT JOIN `users` as u ON u.`id` = o.`users_id`
LEFT JOIN `files` as f ON f.`order_id` = o.`id`
I don't know what is the exact error on the 2nd code, I need to show all columns from the tables: services, users & files
all columns or just defined columns
You when you select * from a join you are selecting all results from all tables involved.
When you are specifying orders you are only getting the results as they pertain to the orders table you get the same thing if you were to do SELECT o.* so if you want to see shared fields from different tables you have to specify them in your select statement as well.
Basically you're seeing the Different between SELECT * and SELECT o.*
This code is working for me, thanks everybody :)
SELECT
o.`id` AS `id`,
o.`service_id`,
o.`extras`,
o.`quantity`,
o.`price`,
o.`links`,
o.`keywords`,
o.`status_id`,
o.`users_id`,
o.`date`,
o.`notes`,
o.`c_reason`,
o.`agent_star`
s.*
u.*
f.*
FROM `order` as o
LEFT JOIN `services` as s ON s.`id` = o.`service_id`
LEFT JOIN `users` as u ON u.`id` = o.`users_id`
LEFT JOIN `files` as f ON f.`order_id` = o.`id`
Is there anyway to select all from 1 table based on the result of one query which contains multiple values without having to do 2 separate queries?
Say long join query will produce a list of id's
SELECT xyz FROM table long join query WHERE id = array of ids found in result table
added example:
SELECT * FROM tweets as t
where t.user_id
in(
SELECT uff.id, uff.username
FROM users as uf
LEFT JOIN followlinks as fl
ON uf.id = fl.user_id
LEFT JOIN users as uff
ON fl.follow_id = uff.id
WHERE uff.id = 1
)
The bit in the parenthesis returns an id and user name of who the user is following (uff.id=1)
How do i then get all 'tweets' by all the id's in the generated resultset
You can use subquery:
SELECT * FROM `table1` WHERE `id` IN (SELECT `table2`.id FROM `table2` )
You might want to check documentation for syntax
SELECT xyz FROM table_A join table_B WHERE table_A.id IN (SELECT ID FROM table_C);
I think you mean something like
edited after the OP edit
SELECT * FROM tweets as t
WHERE t.user_id
in(
SELECT uff.id //remove the second field, you just need the id
FROM users as uf
LEFT JOIN followlinks as fl
ON uf.id = fl.user_id
LEFT JOIN users as uff
ON fl.follow_id = uff.id
WHERE uff.id = 1
)
After trying the in clause I coudnt get the results I was after but after rethinking what I was trying to do I got my results with an additional join clause
SELECT uff.username, t.content
FROM users as uf
JOIN followlinks as fl
ON uf.id = fl.user_id
JOIN users as uff
ON fl.follow_id = uff.id
JOIN tweets as t
ON t.user_id = uff.id
WHERE uf.id = 1
I want to use WHERE in mysql join
SELECT user.name , course.name
FROM `user`
LEFT JOIN `course` on user.course = course.id;
i want to add WHERE name='Alice' in user.name
i tried to do it via :
SELECT user.name WHERE name='Alice' , course.name
FROM `user`
LEFT JOIN `course` on user.course = course.id;
or
SELECT user.name , course.name
FROM user WHERE user.name='Alice'
LEFT JOIN course on user.course = course.id;
but it seam they are wrong
how can i use WHERE in Mysql join ( inner join , left join ,... )
The syntax is wrong, the correct syntax is
SELECT
FROM
JOIN
ON
WHERE
So in your case it will be
SELECT a.name, b.name as bname
FROM `user` a
LEFT JOIN `course` b
ON a.course = b.id
WHERE a.name='Alice'
Note that i used an alias since two columns have the same name
Almost right. This should work fine:
SELECT u.name, c.name
FROM `user` u
LEFT JOIN `course` c on u.course = c.id WHERE u.name='Alice';
I know, it's weird to think about, but that's actually how you do it.
And I changed to u and c because it's neater to work with.
Im not use but may try this.
SELECT user.name AS user_name, course.name AS course_name
FROM user
LEFT JOIN course
ON user.id = course.id
WHERE user.name = 'Alice';
You can create alias for your table or sub-query, then you can simply approach to data in the table.
There is old article but nothing changed in SQL. It will be usefull for you. Third example is solution for your problem.
http://www.sqlteam.com/article/additional-criteria-in-the-join-clause