MySql - how to calculate count(*) for query with group by? - php

I have the query :
select sellers.* from sellers
left join locations
on locations.seller_id = sellers.id
group by sellers.id
limit 0, 10;
Let assume that first query gives me 10 results with a group by, 15 results without a group by.
Now I want to calculate "all results count" for the pagination.
select count(*) from ...
I tried this :
select count(*) from sellers
left join locations
on locations.seller_id = sellers.id
//group by sellers.id;
1.WITH "group by sellers.id" I get 10 results with value 1 (it should be one result with value 10)
2.WITHOUT "group by sellers.id" I get one result with value 15 (it should be one result with value 10)
any ideas?

This should work for you:
select count(*) from (select sellers.id from sellers left join locations on locations.seller_id = sellers.id) as a;

You can put your SELECT query inside a SELECT COUNT(*) query, this way:
SELECT count(*) FROM (select sellers.* from sellers
left join locations
on locations.seller_id = sellers.id
group by sellers.id) sellers;

I don't approve of your query, but you simply want the count of sellers. The simplest method is:
select count(*)
from sellers;

Related

How we can get the data from the table by limiting the number of identical columns MySql

Yesterday I tried to retrieve data from my db table using 'user_id' as a criterion to limit the amount of data per user.
I tried to get data from table https://prnt.sc/p53zhp in format like this https://prnt.sc/p541wk and limit the number of output records for user_id where limit will be 2 (count(user_id) <= 2), but i don't understand how to do that. What kind of sql request can i use to get this data?
Assuming that your RDBMS, here is a solution yo select only the top 2 records per user. You can use ROW_NUMBER() in a subquery to rank records by id within groups of records having the same user_id, and the filter out unerelevant records in the outer query, like:
SELECT *
FROM (
SELECT
t.*,
ROW_NUMBER() OVER(PARTITION BY user_id ORDER BY id)
FROM mytable
) x WHERE rn <= 2
On earlier versions of MySQL, you could use self-LEFT JOIN the table and use GROUP BY and HAVING COUNT(...) < 2 to limit the results to first two records per group:
SELECT
t.id,
t.user_id,
t.vip,
t.title,
t.description,
t.data
FROM mytable t
LEFT JOIN mytable t1 ON t1.user_id = t.user_id AND t1.id > t.id
GROUP BY
t.id,
t.user_id,
t.vip,
t.title,
t.description,
t.data
HAVING COUNT(t1.id) < 2
I don't understand if your problem is a Transact-SQL or your code.
In SQL you can limit record with "LIMIT": https://www.w3schools.com/sql/sql_top.asp
In code, you can use a condition IF.

Count for two column from a table using groupby or unionall based on users

I want count for two columns from a single query which are in same table.
1- followerscount
2- followeecount
I am using union and groupby to get count for two columns but only able to get only one column count from query.
Query I used below:
SELECT follow_attr.username, follow_attr.followerid, follow_attr.followeeid, follow_attr.followercount, follow_attr.followeecount
FROM (
SELECT u.username, dff.followerid AS followerid, dff.followeeid AS followeeid, count(dff.followerid) AS followercount, count(dff.followeeid) AS followeecount
FROM tablename AS dff
LEFT JOIN user AS u ON u.userid = dff.followerid
GROUP BY dff.followerid
UNION ALL
SELECT u.username, dffs.followerid AS followerid, dffs.followeeid AS followeeid, count(dffs.followerid) AS followercount, count(dffs.followeeid) AS followeecount
FROM tablename AS dffs
LEFT JOIN user AS u ON u.userid = dffs.followeeid
GROUP BY dffs.followeeid
) AS follow_attr
WHERE follow_attr.username IS NOT NULL
GROUP BY follow_attr.followerid, follow_attr.followeeid
ORDER BY RAND()
LIMIT 0,6
I am not been able to get followeecount in this query.
Let me know whats wrong in that procedure.
Do this
SELECT COUNT(followercount) AS f_count, COUNT(followeecount) AS fe_count FROM tablename

MySQL: Query design issue

My tables are like this:
Table 1 (students)
Table 2 (results)
I want to select all students from Table 1 students who have 4 results in the results table. I tried this query, but with no success:
SELECT *
FROM students
WHERE gender = 'm'
AND (SELECT COUNT( result ) AS count
FROM results
INNER JOIN students ON results.stuID = students.stuID
WHERE result !=0
) =4
ORDER BY rank ASC
You can rewrite your query by using join and HAVING clause to check the count for each student group ,This can be done without using the subquery which sometimes affects on performance
SELECT s.*,COUNT(*) AS count
FROM students s
INNER JOIN results r ON r.stuID = s.stuID
WHERE r.result !=0
GROUP BY s.stuID
HAVING count =4
ORDER BY s.rank ASC
um, that's a little convoluted.
the where clause should come after the subquery, and the subquery still needs to be JOINed back to the main query.
something like
SELECT * FROM students
INNER JOIN (SELECT COUNT(result),results.stuID as count FROM results WHERE result != 0) as result_count
ON result_count.stuID = students.stuID
WHERE result_count.count =4 AND students.gender = 'm'
ORDER BY rank ASC
You have to use alias for table also -
SELECT *
FROM students as a
WHERE gender = 'm'
AND (SELECT COUNT(result) AS count
FROM results as b
WHERE b.stuID = a.stuID AND
(result!=0 OR result IS NOT NULL OR result!='')
) = 4
ORDER BY rank ASC

Confused on how to combine results from two queries

Query 1:
SELECT count(id) as conversions, SUM(user_payout) as amount, geoip.country_name, geoip.country_code
FROM conversions
LEFT JOIN geoip ON conversions.end_ip BETWEEN geoip.start_long AND geoip.end_long
WHERE user_id = 1
AND type != ''
AND status = 1
AND month(created_at) = month(now())
GROUP BY country_name
ORDER BY country_name ASC
Results:
Query 2:
SELECT count(id) as clicks, geoip.country_name, geoip.country_code
FROM clicks
LEFT JOIN geoip on clicks.ip BETWEEN geoip.start_long AND geoip.end_long
WHERE user_id = 1
AND month(created_at) = month(now())
GROUP BY country_name
ORDER BY country_name ASC
Results:
I'd like to combine the two so for each country, it shows clicks, conversions, country_name and country_code.
Can't figure out how to do this.
Thanks!
You can combine them using subqueries and left outer join:
select clicks.country_code, clicks.country_name,
coalesce(clicks.clicks, 0) as clicks,
coalesce(conversions.conversions, 0) as conversions
from (<subquery 2>) clicks left outer join
(<subquery 1>) conversions
on clicks.country_code = conversions.country_code;
This assumes that all countries have at least one click.
EDIT:
If the list of countries is in geo_ip, you can do:
select gi.country_code, gi.country_name, clicks.clicks, conversions.conversions
from (select distinct country_code, country_name
from geo_ip
) gi left outer join
(<subquery 2>) clicks
on clicks.country_code = gi.country_code left outer join
(<subquery 1>) conversions
on conversions.country_code = gi.country_code;
Left Join clicks on the first query to geoip.
Or you could use a union but you would have go get each result to only show clicks, conversions, country_name and country_code by only selecting those in the select statement.

How to get results count from each entry ID - PHP/SQL?

here is a small sample of what im trying.. as its hardly to code it here I have uploaded an image..
http://i46.tinypic.com/5bxn5u.png
just want to display the total number of reservations for each room.. thanks
You'll want to join the tables together and use COUNT() with a GROUP BY:
SELECT
a.id, a.name, COUNT(*) AS num_reservations
FROM
availables a
LEFT JOIN reservations r
ON r.available_id = a.id
GROUP BY
a.id
This should give you a list of each room with a total number of reservations each has.
To put it into your existing query, you can use the following:
$coordinator = (isset($_GET['uidse']) && is_numeric($_GET['uidse'])) ? (int)$_GET['uidse'] : 0;
$result = mysql_query("SELECT *, COUNT(*) AS num_reservations FROM availables LEFT JOIN reservations ON availables.id=reservations.available_id WHERE coordinator='" . $coordinator . "' GROUP BY availables.id");
I placed $coordinator outside of the MySQL query and added very basic validation/sanitization to save you from a SQL-Injection headache in the future.
SELECT availables.name, COUNT(*) FROM availables
LEFT JOIN reservations ON reservations.available_id = availables.id
GROUP BY reservations.available_id

Categories