relating two MySQL tables with followers - mysql

Good afternoon, I'm not very expert in mysql.
but I would like to relate my table followers to show.
Here is an example of what I need to do.
thank you very much
table number1
id | user | example
--------------------------
1 | john | tall
2 | dave | fat
3 | maria | pretty
4 | example | love
4 | andres | hope
table number2 followers
id | id_user | user_table1
--------------------------
1 | fran | red
2 | love | dave
3 | maria | dave
4 | maria | dave
5 | maria | dave
selet * from number1 where user = 'dave'
result:
2 | dave | fat
but I would like to relate the table number2 and number2.
I need this
result:
1 | dave | fat
2 | maria | pretty
3 | example | love

I'm not quite sure the relation you need actually but based on your example. I think you need all field which related to 'dave'. Try this :
select distinct a.id, a.user, a.example
from number1 a inner join number2 b
on a.user = b.user_table1
or a.user = b.id_user
or a.example = b.id_user
where b.user_table1 = 'dave'
order by a.id
Example : http://sqlfiddle.com/#!9/9f2f79/16

Related

SQL JOIN 2 table

i have 2 table like this
student
|-----------------------|
| id | name | value |
|-----------------------|
| F01 | Ruben | 4 |
| F02 | Dani | 2 |
| F03 | Mike | 3 |
| F04 | John | 4 |
|-----------------------|
tutor
|-------------------------|
| id | code | student_id |
|-------------------------|
| 1 | S2244 | F01 |
| 2 | S3251 | F02 |
| 3 | S2244 | F03 |
| 4 | S2244 | F04 |
|-------------------------|
note, tutor.code ( S2244 and S3251) is foreign key from another table, tutor.student_id is foreign key from student table, how to make the two tables combined and produce a result as below?
|-----------------------|
| id | name | value |
|-----------------------|
| F01 | Ruben | 4 |
| F03 | Mike | 3 |
| F04 | John | 4 |
|-----------------------|
the result is the same as the student table, but the data is released based on what is stored in the tutor table, in the tutor table there is code "S3251" / "F02" which is not displayed in the results table
this is like the "WHERE" condition but the WHERE 'condition ' is used in other tables, I've tried using JOIN but i can't, or maybe my table design is wrong? Please help, this code that I made but didn't get a good result
SELECT st.id, st.name, st.value FROM student st JOIN tutor tt ON tt.code = 'S2244'
You can use exists:
select s.*
from students s
where exists (
select 1 from tutor t where t.student_id = s.student_id and t.code = 'S2244'
)
This makes more sense than a join, since you are not selecting from the tutor table).
The problem with your original attempt is that you are missing a joining condition on student_id (which is common to both tables). The syntax would be:
select s.*
from student s
inner join tutor t on s.id = t.student_id
where t.code = 's2244'
In addition to #GMB's answer above, which is absolutely correct, you could also do the following, which I think might be more intuitive for a beginner developer:
select * from student
where id in (
select student_id from tutor where id = 's2244'
)

How can I match common rows?

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

mysql - Limit a query with left join

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!

Adding sum from 2 different tables

I have something like this
2 tables:
videos
members
In the members table I have the name of each member:
1 Tom
2 Bob
3 Zack
4 Dan
5 Casey
In the videos table I have a column named members and I have the names in there seperated by commas
1. Tom,Dan
2. Casey,Zack,Bob
3. Tom,Casey,Dan,Zack
4. Zack,Bob,Dan
I'm trying to display how many times each member appears to get these results:
1 Tom = 2
2 Bob = 2
3 Zack = 3
4 Dan = 2
5 Casey = 2
Do I need to do something like SELECT SUM(members) WHERE and use LIKE?
I would strongly suggest to normalize your data as others suggested.
Based on your current design you can use FIND_IN_SET to accomplish the result you want.
SELECT
M.id,
M.name,
COUNT(*) total
FROM members M
INNER JOIN videos V ON FIND_IN_SET(M.name,V.members) > 0
GROUP BY M.name
ORDER BY M.id
See Demo
Running this query on your given data set you will get output like below:
| id | name | total |
|----|-------|-------|
| 1 | Tom | 2 |
| 2 | Bob | 2 |
| 3 | Zack | 3 |
| 4 | Dan | 3 |
| 5 | Casey | 2 |
A must read
Is storing a delimited list in a database column really that bad?
More
This is how your vidoes table would look like if you normalize your data:
videos
id member_id
One way to go is to join the two tables, based on a like expression:
SELECT members.name, count (*) as counter from members inner join videos
ON videos.members like CONCAT('%,',members.name,',%')
GROUP BY members.name;
But I think the better solution will be like #e4c5 said in the comment - you need to normalize the data. the videos table should look like:
+---+-------+
|ID | MEMBER|
+---+-------+
| 1 | Tom |
| 1 | Dan |
| 2 | Casey |
| 2 | Zack |
| 2 | Bob |
| 3 | Tom |
| 3 | Casey |
| 3 | Dan |
| 3 | Zack |
| 4 | Zack |
| 4 | Bob |
| 4 | Dan |
+---+-------+
That way, you can simply count on this table

Group by Where(field.id is the same)

Table header
x---id---x---short_name---------x
| 1 | alcatel |
| 2 | Nexus |
| 3 | ZTE |
x--------x----------------------x
Table Detail
x---id---x---------code---------x---header.id---x
| 1 | XXX | 1 |
| 2 | ZZZ | 2 |
| 3 | ZZZ | 2 |
| 4 | XXX | 3 |
x--------x----------------------x---------------x
And I need to GROUP BY CODE Where field.id is the same, example:
In this case I get 2 rows with same field.id so I need to group by my field code just in case if field.id is the same..
(Very hard to me to explain...)
I need to get this
x---header.id---x--detail.id--x-----short_name----x---detail.code---x
| 1 | 1 | alcatel | XXX |
| 2 | 3 | Nexus | ZZZ |
| 3 | 4 | ZTE | XXX |
x---------------x-------------x------------------x-----------------x
I don't know if this is actually possible to do, but if it is please help me, if it isn't please tell me that this is impossible.
EDIT: my field.id and field.code (the ID and CODE fields) are from a table with a relashion 1 to N called field
select h.id as header_id, max(d.id) as detail_id, h.short_name, d.code
from header h
join detail d on d.`header.id` = h.id
group by header_id, h.short_name, d.code
Try this
SELECT MAX(id) AS id, `field.id`, code
FROM test
GROUP BY code, `field.id`
ORDER BY id ASC
Check SQL Fiddle