Select unique ids from multi values on one column in another table - mysql

I'm stuck for hours on an issue that might be pretty simple to solve but I'm just so lost...
I got 3 tables :
user
id name
----------
1 jack
2 john
...
car
id name
----------
1 ford
2 fiat
3 alfa
4 lada
...
user_car
id_user id_car
-----------------
1 2
1 4
2 1
2 2
2 3
For example, i want to get all users with cars which have id 1 AND 2 in the user_car table so I should get the id_user 2 only and I can't find the proper way to do it.

try this untested query:
select * from user join user_car car1 on id =car1.user_id
join user_car car2 on id =car2.user_id where car1.id_car=1 and car2.id_car=2

I would use UNION for this matter:
SELECT id as id_user from user where id in(1, 2)
UNION
SELECT id as id_car from car where id in (1, 2)

You can use COUNT to do this:-
SELECT user.name
FROM user
INNER JOIN user_car ON user.id = user_car.id_user
INNER JOIN car ON user_car.id_car = car.id
WHERE car.id IN (1,2)
GROUP BY user.name
HAVING COUNT(DISTINCT car.id) = 2
Note that this could be simplified to remove the need for the car table when you are just using the id of the car.

May be you want this
SELECT *
FROM USER
WHERE id IN
(SELECT id_user
FROM user_car
WHERE id_car=1 OR id_car=2);

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

MYSQL, How to compare ID with other column in the same table

I have a table in which I want to compare an ID column with another column in the same table.
This is my table
ID | Name | BossID
1 John 3
2 Max 4
3 Peter 4
4 Alex 5
For example I want to use
select * from mytable where ID = BossID
and I expected to get that Peter is the Boss of John and Alex is the Boss of Max and Peter, but when I use it this way, I dont get any info on the query...
Any idea to get it?
The expected query result are:
ID | Name | BossID
1 John 3
2 Max 4
3 Peter 4
seems like you want the list of employees who their boss exists in the list as well:
select *
from mytable
where bossid in (select id from mytable);
to get the boss name :
select t.ID, t.Name, b.Name as BossName
from mytable t
join mytbale b
on t.bossid = b.id
you can use left join to return all the employees in the list

How to select data from one table and select count to another table and combine it in MySQL?

In one fruit there can be multiple tickets that can be raised. I need to display the number of tickets raised per fruit. Their key field is the fruit_id.
If I have the following tables:
fruit
id name
1 apple
2 orange
tickets
id fruit_id
1 1
2 1
3 2
4 2
5 2
Then I would use the following SQL syntax to output a table like the one you need:
SELECT fruit.id, fruit.name, COUNT(tickets.id)
FROM tickets
LEFT JOIN fruit ON fruit.id = tickets.fruit_id
GROUP BY fruit.id;
Output:
id name COUNT(tickets.id)
1 apple 2
2 orange 3
SELECT Fruit.Fruit ID,Fruit.Fruit Name, count(Ticket.Ticket Id) as match_rows FROM Fruit LEFT Join Ticket on (Fruit.Fruit ID = Ticket.Fruit ID) group by Fruit.Fruit Id ORDER BY Fruit.Fruit ID DESC

select rows in mysql having another column with same value

I have a table that manage conversations of a chat between users, the structure is the following.
id | user_id | conversation_id
let's say that on the conversation with ID 1 there are 3 people to chat and the conversation with ID 2, 2 people as well
Conversations_users table will look like this
id | user_id | conversation_id
1 1 1
2 2 1
3 4 1
4 3 2
5 4 2
Now having only the id of the users 3 and 4 and Not Conversation ID I would like select the conversation that belongs to that users so a verbal query should be:
Select from conversations_users, where in user_id = 3 and 4 and conversation_id is equals to conversation id of user 3 and 4
how can I build this "verbal query" in Mysql?
to get all the users in the conversations that user 3 and 4 are part of you could use this:
select distinct(user_id) from conversation_table where conversation_id in (select distinct(conversation_id) from conversation_table where user_id in (3,4));
it won't be very fast though
to get their actual conversations, I'm assuming you have a different table with the text in it:
you probably want something like this
select distinct(u.user_id), c.text from conversation_table u left join conversations c on c.id=u.conversation_id where u.conversation_id in (select distinct(conversation_id) from conversation_table where user_id in (3,4));
here is an sqlfiddle
Here is one method:
select uc.conversation_id
from UserConversions uc
where uc.user_id in (3, 4)
group by uc.conversation_id
having count(*) = 2;
If the table could have duplicates, you'll want: having count(distinct user_id) = 2.
EDIT:
If you want a specific list, just move the where condition to the having clause:
select cu.conversation_id
from conversations_users cu
group by cu.conversation_id
having sum(cu.user_id in (3, 4)) = 2 and
sum(cu.user_id not in (3, 4)) = 0;
I assume you have another table called "conversations" which holds the data you really want.
SELECT *
FROM conversations, conversations_users
WHERE conversations_users.user_id in (3,4)
AND conversations.id = conversations_users.conversation_id

JOIN on array of ids in sql

I have three tables:
TABLE 1 contracts
-------------------
id | contract_name
--------------------
1 test name
2 test name 2
2 test name 3
4 test name 4
TABLE 2 rooms
-------------------
id | room_name
--------------------
1 test name
2 test name 2
2 test name 3
4 test name 4
TABLE 3 promos
----------------------------------
id | contracts_id | rooms_id
----------------------------------
1 1,3 1,3,4
1 2 1,2,3
No I am trying to do an inner join to get the names of the contract and the rooms according to the ids in the array saved in database. I know this is not ideal at all, but I can not change the database set up. So here is what I would like to do with my query, but obviously it is impossible. Does anyone have any idea on how I can accomplish this?
mysql_query("SELECT pb.*, c.contract_name, r.room_name FROM promo_blackouts AS pb
INNER JOIN contracts as c ON c.contract_id IS IN pb.contracts_id
INNER JOIN rooms as r ON r.room_id IS IN pb.rooms_id
WHERE pb.promo_id = '$promo_id'") or die(mysql_error());
Are you looking for something like this?:
SELECT DISTINCT
contract_name,
room_name
FROM
promos
INNER JOIN
contracts ON FIND_IN_SET(contracts.id, contract_id) != 0
INNER JOIN
rooms ON FIND_IN_SET(rooms.id, room_id) != 0
WHERE
promos.id = 1