I have two tables. And my request return me zero rows if my second table is empty and first - isn't... How can I solve this problem?
Table: users
id username name email
1 myuname myname myemail#domain.com
Table: accounts
customerid phone params
1 +1111 NULL
My sql request is below:
SELECT
A.phone,
A.params,
U.email,
U.username,
U.name
FROM `account` A, `users` U
WHERE A.customerid = U.id LIMIT 1';
The request above return zero rows if my account table is empty and users table isn't...
How can I solve this problem?
Thanks.
You can use a LEFT JOIN:
SELECT
A.phone,
A.params,
U.email,
U.username,
U.name
FROM
`account` A LEFT JOIN `users` U ON A.customerid = U.id
LIMIT 1
A LEFT JOIN will select all rows from the first table and only the rows on the second table that matches. If there is no match, U.email, U.username and U.name will be NULL.
You need to LEFT JOIN your tables.
'account' a LEFT JOIN 'users' u ON u.id = a.customerid
Try this
SELECT
A.phone,
A.params,
U.email,
U.username,
U.name
FROM account a
LEFT JOIN users as u ON u.id = a.customerid;
Related
I have fours tables and I wanted to join all three tables with the one table.
I have listed my problem as follows:
Tables:
users
user_training
user_courses
user_certificates
I wanted to get the data from [2,3,4] tables that user_id field matches with the users table ID field.
When I try the INNER JOIN it gives me the result for users that are common in all the tables, But I just wanted to check the [2,3,4] tables with the table [1] Records.
My Query...
SELECT A.training_name AS 'training_name', C.course_name AS 'course_name', D.certificate_name AS 'certificate_name'
FROM user_training AS A INNER JOIN users AS B ON A.user_id=B.ID INNER JOIN user_courses AS C ON B.ID = C.user_id INNER JOIN user_certificates AS D ON B.ID = D.user_id;
Thanks in Advance.
use left join
select u.* from users u
left join user_training ut on ut.user_id=u.user_id
left join user_courses uc on uc.user_id=u.user_id
left join user_certificates uct on uct.user_id=u.user_id
With this one you are getting all users and their respective trainings:
SELECT *
FROM `users`
LEFT JOIN `user_training` ON `users`.`id` = `user_training`.`user_id`
Changing *_trainig to *_courses or *_certificates will return all users with respected courses or certificates.
If you need to get data in one query, try this one:
SELECT *
FROM `users`
LEFT JOIN `user_training` ON `users`.`id` = `user_training`.`user_id`
LEFT JOIN `user_courses` ON `users`.`id` = `user_courses`.`user_id`
LEFT JOIN `user_certificates` ON `users`.`id` = `user_certificates`.`user_id`
If user has no trainings, courses, certificates all remaining fields will be null-ed.
I have SQL tables for users and relationships, like
users{ID, name, email}
relationships{ID, uid, relid, type}
I want to get all "friends" ID, name and email (by current user ID) by SQL, using JOIN or whatever.
If using join is it better to JOIN the table of users and select relationships or select relationships and join users?
something like this (I invented now, i use a bit diferrent tables and code, sorry for errors)
1:
SELECT DISTINCT u.ID, u.name, u.email FROM users u JOIN relationships r ON (r.uid = 1 OR r.relid = 1) AND r.type = 1 WHERE u.ID = r.uid OR u.ID = r.relid
2:
SELECT DISTINCT u.ID, u.name, u.email FROM relationships r JOIN users u ON r.uid = u.ID OR r.relid = u.ID = 1 WHERE r.uid = u.ID OR r.related = u.ID
Edit:
I use DISTINCT becouse when I select users whoose IDs equals to r.uid or r.related, I also get the current user (uid + related) for every relation. DISTINCT should unique the users? I'm beginner and found this on stackoverflow.
So, is it better to select from users and join relations or select relations and join users?
BTW: I quite like the answer with UNION
You can union the users where user is in the uid and in the relid.
SELECT users.*
FROM users
INNER JOIN relationships AS rel
ON users.ID = rel.uid
WHERE users.ID = 500
UNION
SELECT users.*
FROM users
INNER JOIN relationships AS rel
ON users.ID = rel.relid
WHERE users.ID = 500
Not sure why you are using DISTINCT but it looks like you just want to select all the rows from the relationships table (the friends), and then join their details from the users table.
SELECT u.id, u.name, u.email
FROM relationships r
LEFT JOIN users u ON u.id = r.uid
WHERE r.uid = 12345
I am working on social networking site.
I've three tables one is user table which is used to store user details, another table is follow table which is used for followers following list.
In this table I am storing user_id and follower_id.
Third table is user_friends in this I'm storing user_id and friend_userid.
I want to search the user from my friends list and follow list. For this i've written query like this:-
select f.follower_id,uf.friend_userid,u.user_id,u.first_name,u.last_name from tbl_user u
LEFT JOIN tbl_userfriends uf ON uf.friend_userid = u.user_id
LEFT JOIN tbl_follow f ON f.follower_id = u.user_id
where uf.friend_userid != '11'
AND u.first_name LIKE '%a%'
This query returning users only who are friends it is not returning the follow users.
Any help will be appreciated. Thanks in Advance.
You're joining tbl_follow on the Follower ID being equal to the User ID. I suspect that's probably not right.
If you don't already have one you'll need a user id key in the follower table to join on, then you can change your join to;
LEFT JOIN tbl_follow f ON f.userid = u.user_id
i've done this by using following query:-
select u.user_id,u.first_name,u.last_name from tbl_user u LEFT JOIN tbl_userfriends uf ON uf.friend_userid = u.user_id LEFT JOIN tbl_follow f1 ON f1.follower_id = u.user_id LEFT JOIN tbl_follow f2 ON f2.user_id = u.user_id where (uf.user_id = '11' OR f1.user_id = '11' OR f2.follower_id = '11') AND (u.first_name LIKE '%s%' OR u.last_name LIKE '%s%') AND u.status = '0' group by u.first_name
This query returning me all the users who are my followers, friends and to whom i am following.
I have two tables. I want to draw a sample of the first table except where the person in the first table is also in a second table. Am having trouble doing this seemingly simple query.
table users
id|name
table catuser
id|userid|catid
I have tried
SELECT u.*,c.userid FROM `users` u
LEFT JOIN `catuser` c
ON (u.id = c.userid AND c.userid <> '197')
WHERE u.id = '1'
and variations to no avail. Would appreciate any suggestions.
How abt. this:
SELECT u.*,c.userid
FROM `users` u
LEFT JOIN `catuser` c
ON u.id = c.userid
WHERE u.id = '1'
AND c.userid <> '197'
AND c.userid is null
SELECT * FROM users WHERE id NOT IN (SELECT DISTINCT userid FROM catuser)
If you want to query only users that have one or more categories, you can use a WHERE EXISTS query:
SELECT u.* FROM `users` u
WHERE EXISTS (SELECT * FROM catuser WHERE catuser.userid = u.id)
Another possibility is to do a left join, and check whether the join succeeded on checking on null:
SELECT u.*, c.* FROM `users` u
LEFT JOIN catuser c ON u.id = c.userid
WHERE c.id IS NOT NULL
If there is no corresponding row in catuser, all catuser fields will be null. By checking whether c.id is not null, you only include the rows with a category.
Note that the join may return a user multiple time, if he is in multiple categories.
I have these two tables:
1. users
-id
-name
-email
-rank
2. pages
-user
-id
I need to select all the data from users and count how many pages did he wrote (from pages user is the id of the user from users table).
Can you make it into 1 complex query?
select u.*,count(p.*) from users u left join pages p on u.id=p.user group by u.id;
I can't make it into a complex query, but here's a simple one:
SELECT u.name, u.email, u.rank, u.id, count(p.id) as 'PageCount'
FROM Users u
JOIN Pages p ON
p.id = u.id
GROUP BY u.name, u.email, u.rank, u.id
Yes, do it like this:
select u.id, u.name, u.email, u.rank, count(p.id) as 'counter' from users u
inner join pages p on p.user = u.id
group by u.id, u.name, u.email, u.rank
You group by the users info and calculate (count) the number of pages for each user.
How about this.
select u.id, count(p.id) as 'number_of_pages_wrote'
from users u inner join pages p
on u.id = p.id
group by u.id;
You can do it like this -
SELECT u.*, p.count
FROM users u,
(SELECT pa.id, count(1) FROM pages pa WHERE pa.id = u.id) p
WHERE u.id = p.id
What about this simple query
SELECT user, COUNT(*) AS pages FROM page GROUP BY user
select users.id, users.name,users.email,users.rank, count(pages.id)
from users,pages
where users.id = pages.user
group by users.id
This can be accomplished by a JOIN.
For instance,
SELECT * FROM pages
INNER JOIN users ON (users.id=pages.user)
WHERE users.id=5
will select all pages from userid 5. I'll let you do the rest to include the aggregate function ;)