I'm stack in the following: I have 2 tables: Users and Follow. Users table contains 2 columns: ID and Fullname. Follow table contains 2 columns: Follower_ID and Following_ID. It shows that Smith is following Jane.
Now I want an output that shows the full names of both users.
PLEASE HELP!
Table Users:
ID | 1, 2
Fullname | Smith, Jane
Table Follow:
Follower_ID | 1
Following_ID | 2
Required Output:
Follower_ID | 1
Follower_fullname | Smith
Following_ID | 2
Following_fullname | Jane
You can use twice inner join with user table
select
a.Follower_ID
, c.Fullname as Follower_Fullname
, a.Following_ID
, b.Fullname as Following_Fullname
from Follow as a
inner join Users as b on b.ID = a.Following_ID
inner join Users as c on c.ID = a.Follower_ID
Related
I have two arrays like this,
First Table (infos):
------------------------------
| ID | User1 | User2 | User3 |
------------------------------
| 1 | 20 | 30 | 12 |
------------------------------
Second Table (Users):
---------------------
| ID | Name | Email |
---------------------
| 12 | Test | Test# |
---------------------
| 20 | Bla | Test# |
---------------------
| 30 | Bate | Test# |
---------------------
I want to get the information of users on one row from the IDs on the first table.
I try by getting The row from the first table and fetching on users, but I want to optimize the function with just one Query.
SELECT * FROM infos;
SELECT * FROM Infos i,Users u WHERE u.ID = u.User1 (or 2 ...)
Is there any solution ?
You could use joining the table users 3 times, one for each userid you want show the related name (or other values):
select a.id
, a.user1
, b.Name as user1name
, a.user2
, c.name as user2name
, a.user3
, d.name as user3name
from infos a
inner join Users b on a.user1 = b.id
inner join Users c on a.user1 = c.id
inner join Users d on a.user1 = d.id
And just as suggested, you should not use old implicit join syntax based on comma-separated table names and where clause, you should use (since 1992) explicit joins. This syntax performs the same query, but is more clear.
This is a design error. Use a N:N relation (an additional table) to allow any number of users for the first table. With the relation, other queries will be easier.
A relation table looks like this:
create table relation
(
table1_id int unsigned not NULL,
table2_id int unsigned not NULL,
primary key(table1_id,table2_id)
);
A typical query (and I dislike a.* generally):
select a.*, b.*
from table1 a, table2 b, relation r
where r.table1_id = a.id
&& r.table2_id = b.id
I have three mysql tables, category,students and student_category. for each student there is 1 or more category will be there and it is stored in student_category as follows.
1) Categgory
----------------------------
id | category_name
---------------------------
1 | A
2 | B
3 | C
4 | D
2) Students
--------------------------
id | name
--------------------------
1 | John
2 | Kumar
3 | Ashok
4 | Jorge
5 | Suku
-------------------------
2) student_category
-----------------------------------------
id | student_id | category_id
-----------------------------------------
1 | 1 | 2
2 | 1 | 4
3 | 2 | 3
4 | 2 | 1
5 | 3 | 2
------------------------------------------
I need to select students which contain category_id 2 and 4.
i used query as follows but it return either students contain category 2 or category 4.
select A.name from students A, student_category B where A.id=B.student_id
and B.category_id IN (2,4)
Try this query:
SELECT t1.id,
t3.name
FROM students t1
INNER JOIN student_category t2
ON t1.id = t2.student_id
INNER JOIN students t3
ON t1.id = t3.id
WHERE t2.category_id IN (2, 4)
GROUP BY t1.id
HAVING COUNT(DISTINCT t2.category_id) = 2
Explanation:
This query joins together the students and student_category tables, and then removes all records which are not category 2 or 4. This means that each student would then only have category 2 and 4 records associated with him. The HAVING clause then restricts further by requiring that a student have two distinct categories, which if true must mean that the student has both category 2 and 4.
Demo here:
SQLFiddle
try this :
select name from Students where id in (select student_id from student_category where category_id in (2,4))
your query is fine btw.
Try this one:
select
s.name
from
Students s,
Categgory c,
student_category sc
where
sc.student_id = s.id
and sc.category_id = c.id
and c.id = 2
and c.id = 4
You can check it on SQL Fiddle.
Have to take distinct student name as it will repeat if a student falls in more than one category.
i have a little problem with MySql query. I have two tables:
Table name "users"
id, profilephoto, sex, name, lastname
Table name "friends"
id, idSender, idReceiver
idSender and idReceiver is value of id users who send friend request, and who get friend request.
Now i need get all users who is not friend with some login user (for example - User ID - 12)
I make this query:
SELECT DISTINCT users.id, users.profilephoto,users.sex,users.name,users.lastname FROM users INNER JOIN friends ON users.id=friends.idSender WHERE (friends.idSender!=12 OR friends.idReceiver!=12)
And I get wrong informations.
If someone have any idea, I will be thankful.
Simple data from table>
Table Users:
id | profilephoto | sex | name | lastname
1 | image1.jpg | 1 | John | Snow
2 | image2.jpg | 2 | Lisa | Test
3 | image3.jpg | 1 | Patric | Test
4 | image4.jpg | 2 | Elizabet | Test
Table friends:
id | idReceiver | idSender
1 | 1 | 2 // Lisa send friend request to John
2 | 2 | 3 // Patric send friend request to Lisa
For Lisa I want to display only Elizabet
For Elizabet I want to display Lisa, John and Patric
For John I need Patric and Elizabet
For Patric I need John and Elizabet
You need to make left join
SELECT * from users u
LEFT JOIN friends f ON u.id = f.idSender OR u.id = f.idReceiver
WHERE u.id = 12
AND f.idSender IS NULL
OR f.idReciever IS NULL
Hope it helps.
I think trying this query will select user has no friends
select u.*
from user u
left join friends f on f.idSender= u.id or f.idReceiver= u.id
where f.id is null
I have a simple table below, User 2 & 3 are following user 1 , user 3 is following user 2
user_follower
user_id | follower_id
-----------------
1 | 2
1 | 3
2 | 3
User
id | name
---------------
1 | Kuldeep
2 | john
3 | Jacob
I need an sql query all the information of user 1 followers along with thier followers count so the expected result for user 1 would be
id name count
2 john 1
3 jacob 0
I am using PHP with mysql so whatever is the optmial way of doing it is welcome.
Thanks in advance
A self join of the user_follower table to construct the relationships, followed by a join to the User table to obtain the names will give you the result you want:
SELECT uf1.follower_id AS id, u.name, COUNT(uf2.follower_id) AS count
FROM user_follower uf1
LEFT JOIN user_follower uf2
ON uf1.follower_id = uf2.user_id
INNER JOIN User u
ON uf1.follower_id = u.id
WHERE uf1.user_id = 1
GROUP BY uf1.follower_id
Click the link below for a running demo:
SQLFiddle
Try as below :
select id,name,count(user_follower.user_id) as count
from user
left join user_follower
on user.id = user_follower.user_id
group by user.id
Check SqlFiddle
I'm having difficulty figuring out how to create the proper syntax for my query.
Here is what i'm pulling. I have 2 tables.
Table 1 : Fields (user_id, name)
Table 2 : Fields (user_id, type, are_code, phone_number).
Table 1 can only have 1 record per user_id.
1 | John Doe
Table 2 can have up to 3 records per user_id:
1 | Home | 123 | 456.4567
1 | Work | 000 | 987.1467
1 | Mobi | 098 | 987.1756
How can i select everything so that my table will result in 1 record pulled like so :
user_id | name | home# | work# | mobi#
I tried this, which duplicates and doubles rows based on amount of entries within Table 2.
SELECT a.user_id,
b.area_code, b.phone_number
FROM users a
INNER JOIN user_contact_phones b ON a.user_id = b.user_id
That unfortunately returned 3 rows which is not good :(.
1 | John Doe | area | home# |
1 | John Doe | area | work# |
1 | John Doe | area | mobi# |
Any help and or pointers would be greatly appreciated.
Try this out:
SELECT
u.user_id,
u.name,
MAX(CASE WHEN p.type = 'Home' THEN phone_number END) HomeNumber,
MAX(CASE WHEN p.type = 'Work' THEN phone_number END) WorkNumber,
MAX(CASE WHEN p.type = 'Mobi' THEN phone_number END) MobiNumber
FROM phones p
JOIN users u ON p.user_id = u.user_id
GROUP BY u.user_id, u.name
Output:
| USER_ID | NAME | HOMENUMBER | WORKNUMBER | MOBINUMBER |
|---------|----------|------------|------------|------------|
| 1 | John Doe | 456.4567 | 987.1467 | 987.1756 |
Fiddle here.
Also note that you can remove u.name if u.user_id determines u.name... which is most likely the case as it seems to be a primary key. That would speed things up a little bit.
Note: This assumes that you cant have more than one same type for the same user (as it is in your example data, which only has one column for home, work and mobile.
Use user_contact_phones.type to get exact what you want, like-
SELECT a.user_id,
b.area_code, b.phone_number
FROM users a
INNER JOIN user_contact_phones b ON a.user_id = b.user_id where b.type='Home'
Here's a solution that will work:
select u.user_id, u.name,
thome.area_code as home_area_code, thome.phone_number as home_phone_number,
twork.area_code as work_area_code, twork.phone_number as work_phone_number,
tmobi.area_code as mobi_area_code, tmobi.phone_number as mobi_phone_number
from table1 u
left outer join table2 thome on u.user_id = thome.user_id and thome.type = 'Home'
left outer join table2 twork on u.user_id = twork.user_id and twork.type = 'Work'
left outer join table2 tmobi on u.user_id = tmobi.user_id and tmobi.type = 'Mobi'
Please note the use of left outer join instead of inner join in case the record for a particular type does not exist. You will get null values for those columns in your result set with left outer join. With inner join, you would not get a result for a user that did not have all three types. Good luck!