use WHERE in mysql join - mysql

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

Related

Combining select queries

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 = ?

SQL not working when I choose some fields from the 1st table

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`

How can I convert this SQL SubQuery into a Join?

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

SELECT categories from a mapping table

I have 3 tables :
categories:
ID, category
"1","Cars"
"2","Trucks"
"3","Bikes"
"4","Planes"
"5","Boats"
users:
ID, username
"1","john"
"2","bob"
"3","billy"
users_categories:
ID, userid, categoryid
"1","1","2"
"2","1","5"
"3","2","3"
"4","3","2"
"5","3","4"
"6","3","5"
Q1. What I want is :
john,Trucks,Boats
bob,Bikes
billy,Trucks,Planes,Boats
I've come to this. A Concat of the categories would do.
SELECT U.`username`, (SELECT C.`category` FROM `categories` C LEFT JOIN `users_categories` UC ON C.`ID` = UC.`categoryid` WHERE U.ID = UC.userid) FROM `users` U
But I get #1242 - Subquery returns more than 1 row.
Q2. Is there a better way to structure this ? There won't be more than 50-100 categories.
use GROUP_CONCAT to achieve what you want
SELECT a.username,
GROUP_CONCAT(c.category)
FROM users a
INNER JOIN users_categories b
On a.Id = b.userID
INNER JOIN categories c
ON b.categoryID = c.ID
GROUP BY a.ID
SQLFiddle Demo
if you can live with having the categories as a comma separated string, you can use the GROUP_CONCAT function.
Let's see (I've never tried myself in mysql)
select u.username,
GROUP_CONCAT(DISTINCT c.Category order by c.Category SEPARATOR ',')
from users u
join usersCategories uc
on u.ID = uc.userID
join Categories c
on c.ID = uc.CategoryID
You might have to adjust it to MySQL specific syntax, sorry.

Mysql joins problem

I use this query to select all articles :
SELECT articles.*,categories.category_name,users.username,tags.tag
FROM articles
LEFT JOIN `categories` ON articles.category_id = categories.category_id
LEFT JOIN `users` ON articles.author_id = users.user_id
LEFT JOIN `tags` ON articles.article_id = tags.article_id
ORDER BY articles.date_added DESC
I have an other table comments, and I want to count how many comments are there, where the article_id in that table = article_id in the articles table. I tried with COUNT, but then it returns only one result. How can I do that with one query?
You can use a subquery in the SELECT clause:
SELECT articles.*,categories.category_name,users.username,tags.tag, (SELECT count(*) FROM comments c WHERE c.article_id = articles.article_id) as comments_count
As arnaud576875 already stated, you can use a subquery to extract the summary data.
Two things I've noticed from your SQL that are not really a part of the question but still worth pointing out.
you can use a table alias to shorten your SQL and make it more readable.
So instead of
SELECT articles.*,categories.category_name,users.username,tags.tag
FROM articles
LEFT JOIN `categories` ON articles.category_id = categories.category_id
LEFT JOIN `users` ON articles.author_id = users.user_id
LEFT JOIN `tags` ON articles.article_id = tags.article_id
ORDER BY articles.date_added DESC
you'd code
SELECT a.*, c.category_name, u.username, t.tag
FROM articles a
LEFT JOIN `categories` c ON a.category_id = c.category_id
LEFT JOIN `users` u ON a.author_id = u.user_id
LEFT JOIN `tags` t ON a.article_id = t.article_id
ORDER BY a.date_added DESC
I would drop SELECT * and select only the fields that you actually are going to use. This also helps with readability of your code.