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.
Related
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
My New Project is something like a blog roll in which users can follow other users.
While listing all Posts, There will be a button to follow/unfollow under each post.
Tables used are users, posts and followers. which are as follows:
USER Table
id firstName lastName
.....................................
1 arun prasanth
2 ann antony
3 sruthy abc
6 John abc
POSTS Table
id user_id postname url
.....................................................
1 2 post1 www.url1.com
2 2 post2 www.url2.com
3 6 post3 www.url3.com
4 3 post4 www.url4.com
Followers Table
id user_id following_users_id date
..............................................
1 2 1 2018-01-25
2 2 3 2018-01-25
3 6 3 2018-01-25
4 3 6 2018-01-25
to list all posts my query was like:
SELECT * FROM posts LEFT JOIN users on posts.user_id = users.user_id;
and it worked fine as both table have corresponding rows and the result was by listing both table columns with all data.
Now My issue is while joining third table, ie followers table. problem with followers table is, it will have only one row which corresponds to current logged in users user_id and followers.user_id.
i tried all variants like LEFT RIGHT INNER and OUTER JOIN's. but no success.
the result i want is:
user.id -> xxx
user.firstName -> xxx
user.lastName -> xxx
posts.id -> xxx
posts.user_id -> xxx
posts.postname -> xxx
posts.url -> xxx
followers.id -> xxx
followers.user_id -> **Logged IN Users ID**
followers.following_users_id -> **posts.user_id**
followers.date -> xxx
the followers.id, followers.user_id, followers.following_users_id, followers.date should be printed only if followers.user_id === Logged IN Users ID AND followers.following_users_id === posts.user_id is true. else those columns should be blank.
Is there any way i can retrieve data as mentioned above of should i use something like a foreach loop?
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.
i need to develop the query with join or any thing i can't make it
my concept is the first table have a usr id and usr type id
the second table have details of usr type and id and usr master details id
the third table have usr master details and id my question is how i select the value of those three table
Sample table is
user_details
usr_id Name us_ty_id
25 john 2
34 sam 3
24 rose 1
user_type
us_ty_id type usr_ma_id
1 dev 2
2 desi 1
3 test 2
user_master
usr_ma_id details
1 team1
2 team2
3 team3
my output like below
usr_id type details
34 test team3
the first table us_type_id find the type and usr_ma_id in second and find the details of selected id of usr_ma_id in second table and find the details
ple help me...
What about the simple:
SELECT *
FROM user_details d
INNER JOIN user_type t ON t.us_ty_id = d.us_ty_id
INNER JOIN user_master m ON m.usr_ma_id = t.usr_ma_id;
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! :-)