I have 2 tables like this:
Table person
id | name
---------
1 | john
2 | mike
3 | carl
4 | keny
5 | anna
Table vehicle
owner | vechicle
----------------
1 | RTA457
3 | GSW684
3 | GKI321
3 | SNE798
5 | YTT662
So, I want to make a query joining both tables, something like this:
SELECT * FROM person LEFT JOIN vehicle ON person.id=vehicle.owner
Getting these results
id | name | owner | vechicle
----------------------------
1 | john | 1 | RTA457
2 | mike | NULL | NULL
3 | carl | 3 | GSW684
3 | carl | 3 | GKI321
3 | carl | 3 | SNE798
4 | keny | NULL | NULL
5 | anna | 5 | YTT662
Finally, I want to limit it to 3 persons, showing all their vehicles, like this:
id | name | owner | vechicle
----------------------------
1 | john | 1 | RTA457
2 | mike | NULL | NULL
3 | carl | 3 | GSW684
3 | carl | 3 | GKI321
3 | carl | 3 | SNE798
There is any way to do it?
May help with a subquery
SELECT
*
FROM
(SELECT * FROM person LIMIT 3) t
LEFT JOIN vehicle ON t.id = vehicle.owner
Didn't try it, but something like this:
SELECT * FROM person
LEFT JOIN vehicle ON person.id = vehicle.owner
WHERE person.id IN (SELECT ID FROM PERSON LIMIT 3);
You could simply have your query as such:
SELECT * FROM person LEFT JOIN vehicle ON person.id=vehicle.owner LIMIT 10;
This SO could be handy as well. Hope this helps!
Related
I need a help with mySQL SELECT query from multiple tables. I have got four tables: school, discipline, pupils and teams.
School table looks like:
+------+---------+---------------+----------+
| id | name | discipline_id | pupil_id |
+------+---------+---------------+----------+
| 1 | one | 2 | 5 |
+------+---------+---------------+----------+
| 2 | two | 3 | 8 |
+------+---------+---------------+----------+
| 3 | three | 4 | 12 |
+------+---------+---------------+----------+
Discipline table looks like:
+------+---------+
| id | name |
+------+---------+
| 1 | math |
+------+---------+
| 2 | bio |
+------+---------+
| 3 | liter |
+------+---------+
| 4 | geo |
+------+---------+
Teams table looks like:
+------+---------+---------------+-----------+
| id | name | school_id | member_id |
+------+---------+---------------+-----------+
| 1 | T1 | 1 | 3 |
+------+---------+---------------+-----------+
| 2 | T2 | 3 | 3 |
+------+---------+---------------+-----------+
| 3 | T3 | 2 | 9 |
+------+---------+---------------+-----------+
The result of disciplines I need to get with a "SELECT from discipline..." query by "member_id = 3" is:
+-----------------+---------------+
| discipline_name | discipline_id |
+-----------------+---------------+
| bio | 2 |
+-----------------+---------------+
| geo | 4 |
+-----------------+---------------+
By matching member's school and then getting its discipline, if it makes sense...Is it possible to do with just one mySQL query?
Type of: member_id 3 => school_id 1,3 => discipline_id = show related disciplines names and ids which are 2, 4
Thank you very much...
Your goal is not clear or makes no sense to me.
But here is what you are literally asking for:
SELECT
s.discipline_id
d.name
FROM teams t
LEFT JOIN school s
ON s.id = t.school_id
LEFT JOIN discipline d
ON d.id = s.discipline_id
WHERE t.member_id = 3
I am having an issues with fetching a particular kind of record from the database.
I have three different tables
Friends
Followers
PictureGalleries
Here is a sample of what the table looks like
Friends:
|id | senderId | receiverId | accepted |
|---|----------| -----------| ---------|
| 1 | 1 | 12 | 1 |
| 2 | 12 | 2 | 1 |
| 2 | 12 | 2 | 1 |
Followers:
| id | userId | UserIsFollowing |
| -- | ------ | --------------- |
| 1 | 12 | 63 |
| 2 | 22 | 12 |
PictureGalleries:
| id | UserId |
| -- | ------ |
| 1 | 13 |
| 2 | 12 |
| 3 | 1 |
| 4 | 10 |
| 5 | 2 |
| 6 | 63 |
So now here is the Issue!
I want to select all from the Picture Galleries
Where the userid has a friendship relationship with userId 12 where accepted is 1
And
Where the userID 12 is following a particular user
So Basically the result I want to see is the picture gallery of the following users ID: 1,2, and 63 which will look like this:
| id | UserID |
| -- | ------ |
| 3 | 1 |
| 5 | 2 |
| 6 | 6 |
use union and sub-query to get desired result
select p.id,p.UserId from
( select UserIsFollowing as id from
Followers fl where userId =12
union select senderId from friends f
where f.receiverId =12 AND accepted=1
union select receiverId from friends f
where f.senderId =12 AND accepted=1
) as t join PictureGalleries p on t.id=p.UserId
I think this query shows what you want:
select * from PictureGalleries
join Followers on Followers.userId = PictureGalleries.UserId
where exists (select 1 from Friends where (Friends.senderId = PictureGalleries.UserId or Friends.receiverId = PictureGalleries.UserId) and accepted = 1)
and Followers.UserIsFollowing = :user_id
But I think your model can be improved
EDIT:
Maybe you said it wrong first when you've said:
"Where the userid has a friendship relationship with userId 12 where accepted is 1
AND Where the userID 12 is following a particular user"
I think you mean OR, so the SQL should be something like:
select * from PictureGalleries
where exists (select 1 from Friends where (Friends.senderId = PictureGalleries.UserId or Friends.receiverId = PictureGalleries.UserId) and accepted = 1)
OR exists (select 1 from Followers where Followers.userId = PictureGalleries.UserId and Followers.UserIsFollowing = :user_id
I have a table like this:
// friends
+----+---------+--------+
| id | user_id | friend |
+----+---------+--------+
| 1 | 1 | Peter |
| 2 | 1 | Martin |
| 3 | 2 | Jack |
| 4 | 1 | Barman |
| 5 | 3 | Peter |
| 6 | 1 | Jack |
| 7 | 3 | David |
| 8 | 2 | David |
| 9 | 3 | Martin |
+----+---------+--------+
Now I have two user_ids. For example 1 and 3. Now I want to match the rows which have common friends. So this is expected result:
| Peter |
| Martin |
Because Peter and Martin are common for both ids 1 and 3.
Is doing that possible by pure sql ?
You can do a self-join of the friends table, with the following three conditions being required to match records from both sides of the join:
The user_id from the first table is 1
The user_id from the second table is 3
The friends match (i.e. are shared by both sides)
SELECT t1.friend
FROM friends t1
INNER JOIN friends t2
ON t1.user_id = 1 AND
t2.user_id = 3 AND
t1.friend = t2.friend
If you have indices setup properly, I would expect this to run faster than an aggregation approach.
Demo here:
Rextester
Try this:
SELECT friend
FROM friends
WHERE user_id IN (1, 3)
GROUP BY friend HAVING COUNT(DISTINCT friend) = 2;
Another way
SELECT friend
FROM t
GROUP BY friend
HAVING SUM(user_id =1)>0
AND SUM(user_id =3)>0
I'm using MYSQL and can't get a NOT LIKE working with multiple JOINS.
I have 3 tables which look like this:
Parents (Table1):
+------------+-------+-----+
| ParentID | Name | AGE |
+------------+-------+-----+
| 1 | Peter | 26 |
| 2 | Karl | 33 |
| 3 | Tessa | 43 |
+------------+-------+-----+
Kids (Table2):
+------------+-------+-----+
| KidID | Name | AGE |
+------------+-------+-----+
| 1 | Mike | 3 |
| 2 | Mike | 13 |
| 3 | Jenna | 4 |
| 4 | Jessi | 14 |
+------------+-------+-----+
Parents_Kids (Table3):
+-----------+-------+
| ParentID | KidID |
+-----------+-------+
| 1 | 2 |
| 1 | 4 |
| 2 | 1 |
| 3 | 3 |
+-----------+-------+
Now i want to get all parent names who don't have a kid named Mike or any form of Mike in there name.
I tried this:
SELECT p.name
FROM PARENTS p JOIN
Parents_Kids pk
ON pk.ParentID = p.ParentID JOIN
Kids k
ON k.KidID = pk.KidID
WHERE k.Name NOT LIKE '%mike%';
But the result is wrong with this query.
If i try this query with LIKE it works correctly but not with NOT LIKE.
I think you don't want to select the parent if atleast one of his/her kids have a name like mike. You can use having to filter such cases.
SELECT p.name
FROM PARENTS p
JOIN Parents_Kids pk ON pk.ParentID=p.ParentID
JOIN Kids k ON k.KidID=pk.KidID
group by p.name
having count(case when k.name like '%mike%' then 1 end) = 0
You say your query works correctly with LIKE, so a way to get the other parent names is this:
SELECT name
FROM parents
WHERE name not in (SELECT p.name
FROM PARENTS p JOIN
Parents_Kids pk
ON pk.ParentID = p.ParentID JOIN
Kids k
ON k.KidID = pk.KidID
WHERE k.Name LIKE '%mike%')
I have one table like this:
+----+---------+-------------+
| id | site_id | search_term |
+----+---------+-------------+
| 1 | 2 | apple |
| 2 | 2 | banana |
| 3 | 3 | cheese |
| 4 | 1 | aubergine |
+----+---------+-------------+
And another like this:
+----+---------+-------------+
| id | site_id | search_term |
+----+---------+-------------+
| 1 | 2 | 1 |
| 2 | 2 | 2 |
| 3 | 2 | 1 |
| 4 | 2 | 1 |
| 5 | 3 | 3 |
| 6 | 1 | 4 |
+----+---------+-------------+
I want to find out how many times each search_term shows up in the 2nd table, and how many times.
In other words, from this data, if I was asking about site_id 2, I'd want this to be returned:
+-------------+-------+
| search_term | count |
+-------------+-------+
| apple | 3 |
| banana | 1 |
+-------------+-------+
I'm familiar with basic joins and such, as well as COUNT, but I'm not sure how to count things from another table.
Thanks!
You could join the tables together, and count the number of rows in the second table:
select t1.search_term
, count(t2.id)
from table1 t1
left join table2 t2
on t1.id = t2.search_term
group by t1.search_term
Give this a try:
select t1.search_term, count(*) from t1
join t2 on t1.id = t2.search_term and t1.site_id = t2.site_id
where t1.site_id = 2
group by t1.search_term