MySQL SQL - How to Query 2 Tables - mysql

table 1
uid email name password
----------------------------------------------
1 abc#abc.com John Doe 9q8wekdshfa
2 xyc#xyc.com Jane Doe a42adsflda2
3 me#meme.com Meme Me asd4q23llsd
table 2
id uid vocation groups
-----------------------------------
1 1 Programmer 1,3,4,5
2 2 Designer 2,4,6,8
3 3 Attorney 3
How do I write the query to get all the data about the user/s that belongs to the same group as the active logged in person. Suppose that I'm logged in as the 3rd account (me#meme.com) and I already have the data that my group is 3. How do I write the query to get this result?
uid email name password vocation groups
----------------------------------------------------------------------
1 abc#abc.com John Doe 9q8wekdshfa Programmer 1,3,4,5

You just need a basic join statement:
SELECT table1.uid, table1.email, table1.name, table1.password,
table2.vocation, table2.groups FROM table1 INNER JOIN table2 ON
table1.uid = table2.uid
WHERE table2.groups LIKE '%3%' AND email<>'me#meme.com'
And certainly read up on JOIN and basic SQL table relationships. This is database 101 stuff, that's the only reason you're getting a bit of grief in the comments and down votes.

Related

Left Join and remove duplicates

I have three tables, clients, job_allocations and jobs table. I want to select all clients that are not in a particular job, below are my tables.
Clients table
id
Fullname
1
John Doe
2
Jane Doe
3
King James
4
Jere Gray
Jobs table
id
Title
1
Road Construction
2
Repair of Engines
job_allocations table
id
client_id
job_id
1
2
1
2
2
2
3
1
2
4
3
2
I want to select all clients that are not in job_id=2, but when I ran my query, I am getting client id: 2 - Jane Doe again, please how do I solve this?
I did this:
LEFT JOIN job_allocations ON job_allocations.client_id = clients.id
WHERE job_id <> 2 OR job_id IS NULL```
You can use a NOT IN clause as follows:
SELECT *
FROM clients
WHERE id NOT IN (SELECT client_id
FROM job_allocations
WHERE job_id = 2)
Check the demo here.
So you will fetch all clients, but only jobs related to job_id <> 2
This query should work for you:
SELECT client.*
FROM clients
LEFT JOIN job_allocations ON job_allocations.client_id = clients.id and job_id <> 2
Use DISTINCT keyword for selecting unique values

Find in set to get the separate values with comma

Before I have asked the same problem (Join table with comma issue (MySQL)) about join table with comma in the column.
I have two tables, table structure like below:
First Table name: student
id | name | course_id
——————————————————————————
1 David 1,3
2 Peter 2,4
3 Shawn 2,6
Second Table name: subject
id | subject
———————————————————
1 English
2 Maths
3 Science
4 Geographic
5 Accounting
6 Art & Design
I have tried this find_in_set method (Search with comma-separated value mysql), but it cannot get the actual result. I want the actual result is like below:
id | name | subject_name
——————————————————————————
1 David English,Science
2 Peter Maths,Geographic
3 Shawn Maths,Art & Design
I am using below code:
SELECT student.id as id,student.name as name,student.subject as subject_name
FROM student
INNER JOIN subject
ON FIND_IN_SET(subject.id, student.course_id) > 0
But the result is shown me like below:
id | name | subject_name
——————————————————————————
1 David English
2 David Science
3 Peter Maths
4 Peter Geographic
5 Shawn Maths
6 Shawn Art & Design
Hope someone guide me on how to solve this problem. Thanks.
Like this
SELECT student.id as id, student.name as name, GROUP_CONCAT(subject.subject) as subject_name
FROM student
INNER JOIN subject
ON FIND_IN_SET(subject.id, student.course_id) > 0
GROUP BY student.id, student.name
Usually we don't concat everything in SQL query, but you can do
SELECT CONCAT_WS(' ', student.id, student.name, GROUP_CONCAT(subject.subject)) as concated_value
FROM student
INNER JOIN subject
ON FIND_IN_SET(subject.id, student.course_id) > 0
GROUP BY student.id, student.name

MYSQL - Select row even if row does not join

I am trying to find one query that returns all people including the company they belong to and companies that do not have any person assigned yet.
Company
cid | cname
--------------
1 Company 1
2 Company 2
Person
pid | pname | fk_company
---------------------------
1 Person 1 1
2 Person 2 1
desired result
pid | pname | fk_company | cid | cname
----------------------------------------------
1 Person 1 1 1 Company 1
2 Person 2 1 1 Company 1
NULL NULL NULL 2 Company 2
Thanks in advance
If you want everything from both tables, regardless of match left AND right, you need a FULL JOIN:
SELECT *
FROM person
FULL JOIN company
ON person.fk_company = company.cid
edit: Apparently mysql doesn't support FULL JOIN. You'll have to do both LEFT JOINS by hand and UNION ALL them.
You should mention something you tried. Anyway, I will explain the method so you can work on it.
SELECT <column_names>FROM <table1_name> LEFT JOIN <table2_name>ON
<table1.column_name> = <table2.column_name>;
For more explanations please refer this link.
SQL Left Join

MySQL IN operator of result from another column

I have 2 tables:
users(uid, name, titles)
titles(uid, name)
users:
uid | name | titles
1 David 2,4
2 John 5
3 Jane 4
titles:
uid | name
2 Owner
4 CEO
5 Manager
The question is how do I select something like this:
SELECT u.* FROM users as u
JOIN titles as t
ON t.uid IN (u.titles)
WHERE t.uid=2
Notice the IN(u.titles)? It's only taking the first title uid in u.titles field. That means when I change condition to WHERE t.uid=4, it shows no records.
Any idea?
SELECT u.*
FROM users as u
JOIN titles as t ON find_in_set(t.uid, u.titles) > 0
WHERE t.uid=2
If you want that each user can have multiple titles I would recommend a reference table which references the users to the titles.

Query with GROUP_CONCAT not working

I have 3 tables with the following structure:
**users**
id
first_name
last_name
**specialties**
specialty_id
specialty_name
**user_specialties**
user_id
specialty_id
Here is some sample data:
**users**
1 Bill Smith
2 Tom Jones
3 Jill Hayes
**specialties**
1 word
2 web
3 database
**user_specialties**
1 1
2 1
2 3
3 2
3 3
I need to query the data so the specialties are concatinated on one row like the below output
**Desired Result**
Bill Smith word
Tom Jones word,database
Jill Hayes web,database
I am using the following query
SELECT
users.first_name,
users.last_name,
GROUP_CONCAT(specialties.specialtyname)
FROM
users
LEFT JOIN user_specialties ON user_specialties.user_id = users.userid
RIGHT JOIN specialties ON user_specialties.specialty_id = specialties.specialty_id
It is not working...
You're missing a GROUP BY clause. Most likely it should be GROUP BY users.id, and it'd go AFTER the JOIN lines.
I just tested this query
SELECT first_name,last_name,group_concat(specialty_name)
FROM user_specialties map
INNER JOIN specialties skill on user.id = map.user_id
INNER JOIN users user ON skill.specialty_id = map.specialty_id
GROUP BY user.id
Cheers! :-)