Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql select count with another select count

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!

like image 635
janusfidel Avatar asked Feb 14 '12 08:02

janusfidel


People also ask

Can we use count and GROUP BY together?

The use of COUNT() function in conjunction with GROUP BY is useful for characterizing our data under various groupings. A combination of same values (on a column) will be treated as an individual group.

Can I use SELECT inside count?

SQL SELECT statement can be used along with COUNT(*) function to count and display the data values. The COUNT(*) function represents the count of all rows present in the table (including the NULL and NON-NULL values).

What is SELECT count (*) as count in MySQL?

COUNT(*) The COUNT(*) function returns the number of rows in a dataset using the SELECT statement. The function counts rows with NULL, duplicate, and non-NULL values. You can also use the WHERE clause to specify a condition.

Which is faster count 1 or count (*)?

The simple answer is no – there is no difference at all. The COUNT(*) function counts the total rows in the table, including the NULL values. The semantics for COUNT(1) differ slightly; we'll discuss them later. However, the results for COUNT(*) and COUNT(1) are identical.


1 Answers

select username, 
       avatar,
       (select count(*) from tbl_entries where user_id = x) as entry_count
from tbl_users 
where user_id = x
like image 148
juergen d Avatar answered Oct 12 '22 04:10

juergen d