MySQL: select last (max) value from column and count of all rows - php

How can I select last (=max) value from column and count of all rows in single query?
ID ITEM_ID VALUE
1 1 100
2 1 101
3 2 201
4 3 333
5 2 222
6 1 111
I want to select last / max value for particular ITEM_ID and count of all rows with this ID.
For ITEM_ID = 1 thus:
VALUE COUNT
111 3
My query is like this:
SELECT (SELECT COUNT(*) FROM table) AS count, (SELECT value FROM table ORDER BY id DESC LIMIT 1) AS value FROM table WHERE item_id = 1 LIMIT 1
It works but looks ... weird. Is there any better (simpler / faster) solution? Thanks

You need to do a GROUP BY on column ITEM_ID while getting the MAX() and COUNT() like
select max(value) as `VALUE`,
count(*) as `COUNT`
from your_table
group by ITEM_ID;

Related

Order by a columns count where another column has a static value

I have a table which stores user items, the two key columns which I would like to use in this query are user_id and item_id. The id field in the example is not needed but just added to show these aren't the only two columns in the table.
----------------------
id user_id item_id
----------------------
1 1 324
2 1 324
3 3 324
4 2 230
5 4 324
The query which I would like to construct should return the top 10 users who have the most items with a specific item id.
So for example if I wanted to run the query against the item ID 324 I should get the following result.
-------------------
user_id item_count
-------------------
1 2
3 1
4 1
2 0
try this
select user_id , count(*) as item_count from table
where item_id = 324 group by user_id order by item_count desc limit 10
limit 10 will show you the top 10 users and order by desc sort from high to low.
However, the above query will not give you the 0 count as per your question. If you really want the zero count you can try this: (assuming your table name is userlist)
SELECT distinct user_id,
(select
count(*) from `userlist`
where user_id=u.user_id and item_id=324
) as item_count FROM `userlist` u
order by item_count desc
I couldn't create the database in my local, but I think this will do the trick
SELECT user_id, COUNT(item_id) as item_count
FROM TABLE_NAME
WHERE item_id = 324
GROUP BY item_id
ORDER BY item_count;

Selecting a distinct value and the sum of times it shows up in a column

I have an existing table with millions of entries (growing) that consists of:
userid|name|etc...
1 frank ...
1 frank ...
2 joe ...
5 sam ...
1 franky ...
What I need to do is return a table of:
place|name|total
1 franky 3
2 sam 1
3 joe 1
Where total is the SUM(userid = the distinct userid).
Currently I'm doing a query to SELECT DISTINCT userid from table and then foreach returned value in php, I'm doing another query to return the name and sum(userid = userid).
As you can assume, this is very taxing and takes a long time now with all of the values. Is there any way to speed this up by doing 1 query?
i think you need
SELECT #a:=#a+1 AS `place`, name, COUNT(userid) AS `total`
FROM `your_table`, (SELECT #a:= 0) AS a
GROUP BY userid
SELECT userid, COUNT(*)
FROM some_table
GROUP BY userid

get the last row of which one of its column value is less than the rows of interest within a set

This is kind of hard to explain so I'll break it down...Here's the objective
Suppose you have a table
ID | Weight
1 2
2 4
3 8
4 66
5 11
6 44
7 33
And suppose I have a set of interested IDs, say (3,4)
My objective is to get two other rows (one for each of the two interested IDs) such that the row that matches with the interested ID has a weight that is one level less than the weight of the interested ID
so in this case
for id 3, we want to return row with ID 2 and weight 4 since row id 2 is the first row of which the weight (4) is less than the weight of row id 3 (8)
for id 4, we want to return row with id 6 and weight 44 since row id 6 is the first row of which the weight (44) is less than the weight of row id 4 (66)
How would you accomplish this with mysql in one single query whereby we use the IN() notation for the interested IDs.....
I'd like to propose the following (used ourtable as table name obviously)
SELECT id,weight FROM ourtable WHERE weight IN (SELECT MAX(t.weight) FROM ourtable t,ourtable t2 WHERE t.weight < t2.weight && t2.id IN (3,4) GROUP BY t2.id);
it gives the following result
+----+--------+
| id | weight |
+----+--------+
| 2 | 4 |
| 6 | 44 |
+----+--------+
as requested.
You could solving this selecting the first row of a selection of the rows ordered by weight desc which weight is lower than the given weight, in this case for mysql something like:
select * from t where weight < (select weight from t where id = :id) order by weight desc limit 1
in a in statement following the idea above, you could have something like:
select * from (select id, (select weight from t where weight < (select weight from t where id = tp.id) order by weight desc limit 1) from t tp) a where id in (3,4)
Another solution w/o subquery:
select w1.id,w1.weight,
left(group_concat(w2.id order by w2.id desc ),LOCATE(',', group_concat(w2.id order by w2.id desc ))-1) as w2_id,
left(group_concat(w2.weight order by w2.weight desc ),LOCATE(',', group_concat(w2.weight order by w2.weight desc ))-1) as w2_weight
from weight as w1, weight as w2
where w2.weight < w1.weight
and w1.id in (3,4)
group by w1.id

problem with result order on distinct select query

i have a table like this
id name date group_id
1 n1 1 1
2 n2 1 1
3 n4 2 2
4 n5 2 2
i want ton write a query to return the group_id without duplicate ordered by date ASC
$query = " SELECT DISTINCT group_id FROM table ORDER BY date ASC";
this query will return 2 , 1 but this query is just going to consider date of the first row of each group_id to order the results
like if i have table like this
id name price date group_id
1 n1 2300 1 1
2 n2 3000 3 1
3 n4 4000 2 2
4 n5 2000 2 2
second row with with '1' as group_id has the biggest date so i should get 1,2 as result but query doesn't care about second row with the '1' group_id and still return 2,1
it only cares about the first row of each id for ordering the results
hopefully there is a easy way to solve this and i dont need to do something wird like putting everything in the 2d array and order that then deleting duplicates
Try this
select group_id,max(date) as SortDate
from table
group by group_id
order by SortDate
If I understand your problem, it sounds like you need to group by the date first.
SELECT group_id
FROM (
SELECT
group_id,
min(date) as min_date
FROM table
GROUP BY group_id
) as t
ORDER BY t.min_date;

MySQL: Group By & Count Multiple Fields

If I have a single field, say, project_id where I want to count the occurrences, I would do something like:
select project_id, count(project_id) as count from tbl group by project_id, order by count desc
What if I want to count the occurrences of a combination of two fields in the same table, i.e. count the number of rows where the combination of project_id and service_id are unique?
So, so I have records in my table like:
project_id service_id
1 1
1 2
1 2
1 2
1 2
1 3
1 4
1 4
1 4
I would want the result of my query to be something like:
project_id service_id count
1 1 1
1 2 4
1 3 1
1 4 3
select project_id, service_id, count(*) as count
from tbl group by project_id, service_id
order by count(*) desc
Just add service_id to your group by and select list. That should do it.
EDIT -
As per comment from #Rajah it seems that for your expected output, you need to use
order by project_id asc, service_id asc

Categories