how can i combine these two queries in mysql?
select count(*) as entry_count from tbl_entries where user_id = x
and
select username, avatar from tbl_users where user_id = x
I want one result that combines the result of this 2 queries. Please help me guys!
Thanks!
select username,
avatar,
(select count(*) from tbl_entries where user_id = x) as entry_count
from tbl_users
where user_id = x
select username,
avatar,
(select count(*) from tbl_entries where user_id = x) AS cnt
from tbl_users
where user_id = x
try this one:
SELECT a.username,
a.avatar,
COUNT(*) as entry_count,
FROM tbl_Users a LEFT JOIN tbl_entries b ON
a.user_ID = b.user_ID
WHERE a.user_ID = x
GROUP BY a.username
Try this:
SELECT U.username, U.avatar,
count(*) AS entry_count
FROM tbl_users AS U
LEFT JOIN tbl_entries AS E ON U.user_id = E.user_id
WHERE user_id = x
GROUP BY U.user_id;
SELECT username,
avatar,
(select count(*) from tbl_entries where user_id = U.user_id) AS cnt
FROM tbl_users AS U
WHERE user_id = x
Related
How do i join two fields from table1 to table 2?
I have this code below, how do i also join fields "mobilenumber","firstname" and "lastname" from the user table into the user_address table?
$query = "SELECT * FROM user_address WHERE user_id IN
(SELECT id FROM user WHERE email = '".$email."')";
$query = "SELECT a.*,b.mobilenumber,b.firstname,b.lastname
FROM user_address a
left join user b on a.user_id=b.user_id
WHERE a.user_id IN
(SELECT id FROM user WHERE email = '".$email."')";
You can use LEFT JOIN instead and select attributes from both table. Example:
$query = "SELECT ua.*,
u.mobilenumber,
u.firstname,
u.lastname
FROM user_address ua
LEFT JOIN USER u
ON u.id = ua.user_id
WHERE u.email = '$email'";
Table -1 : Comment id, comment,user_id,comment Date
Table -2: Users id, user_name, full_name, password
now i want to get user detaiils records who is last comment
like
query is :
select c.*,
(select user_name
from users
where id = c.user_id
) as user_name,
(select full_name
from users
where id = c.user_id
) as full_name
from comment as c, users as u
group by c.user_id
order by comment_date DESC
SELECT Users.*,
Comment.*
FROM Users
INNER JOIN Comment ON (Comment.user_id = Users.id)
GROUP BY Users.id
ORDER BY Comment.id DESC
that should work
Here is your query
select users.* from users inner join comments on
users.user_id = comments.user_id
order by comments.comment_date desc limit 1
Another way to do this
select * from users where user_id =
(select user_id from comments order by comment_date desc limit 1)
How do I get friend list from Friends table with counts of friends of my friends (Count not of my friends)
Friends table"
tbl_users_friends
Field 1: id
Field 2: user_id
Field 3: friend_user_id
and I need the out put as:
A has following friedns:
x (10)
y (2)
z (0)
Above is the list of my friends and in parenthesis contains their friends count.
Thanks
select user_id, count(*) cnt
from Friends
where user_id in
(select friend_user_id
from Friends
where user_id = user_id_of_A)
group by user_id
Try something like this:
select u.user_id, u.name, count(uf1.id) as num_friends
from tbl_users_friends uf
inner join tbl_users u on u.user_id = uf.friend_user_id
left join tbl_users_friends uf1 on uf1.user_id = uf.friend_user_id
where uf.user_id = 1
group by u.user_id, u.name
http://sqlfiddle.com/#!9/10033/1
You need to ajust the users table and column names.
Another solution with a subselect but without group by:
select u.user_id, u.name, (
select count(*)
from tbl_users_friends uf1
where uf1.user_id = uf.friend_user_id
) as num_friends
from tbl_users_friends uf
inner join tbl_users u on u.user_id = uf.friend_user_id
where uf.user_id = 1
For my mobile App i am using a web service to get the images and all records from the MySQL database, its like Instagram.
But SELECT DISTINCT taking more than 7 - 8 seconds.is there any thing that i can do for reduce the time?
SELECT distinct count(*) as totalrecord ,p.description,p.photo_id ,p.user_id,p.imagepath1 ,p.friends, p.is_report_photo,
p.imagepath2,p.imagepath3,p.imagepath4,p.imagepath5,p.count_comment,p.count_like,p.created as photo_created ,
(select username from users where id = p.user_id) as username ,
(select country from users where id = p.user_id) as country,
(select email from users where id = p.user_id) as email ,
(select image_path from users where id = p.user_id) as image_path ,
(select gender from users where id = p.user_id) as gender,
(select privacy from users where id = p.user_id) as privacy,
(select audio_privacy from users where id = p.user_id) as audio_privacy,
(select video_privacy from users where id = p.user_id) as video_privacy,
(select photo_privacy from users where id = p.user_id) as photo_privacy,
If(pl.user_id && pl.photo_id !='',like_status,0) as islike ,
If(pr.user_id && pr.photo_id !='',1,0) as isreport,
p.user_visibility
from friends as fr
inner join users as u on u.id = (select distinct if (fr.user_id != $user_id,fr.user_id,(fr.to_user_id)) as rd
from friends) && fr.read_status = '1'
inner join photos as p on (p.user_visibility = 'p')
LEFT JOIN photolike as pl ON pl.photo_id = p.photo_id && pl.user_id = $user_id
LEFT JOIN photo_report as pr on pl. photo_id = pr.photo_id && pr.user_id = $user_id
ORDER BY p.photo_id DESC;
Use mysql indexing that will reduce the time
Try using group-by clause instead of distinct.
I have a table which includes 130k+ rows. I want to list most liked users.
I try lots of queries but I didnt get result.
table 1 : "users" / user_id / user_name
table 2 : "likes" / datetime / user_id
$query = mysql_query("SELECT COUNT(*) FROM
(
SELECT DISTINCT a.user_id, b.user_id,b.user_name
FROM dbo.likes AS a
INNER JOIN dbo.users AS b
ON a.user_id= b.user_id
) AS subquery;");
and
while($row = mysql_fetch_array($query))
{
echo $row=['user_name'].'-'.$row=['count(*)'];
}
select user_name, count(user_name)
from users, likes
where users.user_id = likes.user_id
group by user_name