I am having trouble counting the number of rows until it reaches a certain PK.
My PK is called id and I want to count all rows until i reach a specified id
I have tried using this query but it doesn't work probably becuase I am using a MySQL table
select max(count(*)) from news where id=18 group by id
I get this error
Invalid use of group function
select count(*) from news where id<=18
I would use the following:
select count(id) from news where id <= 18
This will be more efficient as you are only returning one column in a row as opposed to all of them.
Related
I'm trying to get the highest version within a group. My query:
SELECT
rubric_id,
max(version) as version,
group_id
FROM
rubrics
WHERE
client_id = 1
GROUP BY
group_id
The Data:
The Results:
The rubric of ID 2 does not have a version of 2, why is this being mismatched? What do I need to do to correct this?
Edit, not a duplicate:
This is not a duplicate of SQL Select only rows with Max Value on a Column , which is a post I have read and referenced before writing this. My question is not how to find the max, my question is why is the version not matched to the correct ID
MySQL is confusing you by letting you get away with having a column in your select that isn't in your group by. To resolve the issue, make sure you don't select any field that isn't in the group by.
Instead of trying to get everything in one statement, you will need to use a subquery to find the max_version_id and then join to it.
SELECT T.*
FROM rubrics T
JOIN
(
SELECT
group_id,
max(version) as max_version
FROM
rubrics
GROUP BY
group_id
) dedupe
on T.group_id = dedupe.group_id
and T.version_id = dedupe.max_version_id
WHERE
T.client_id = 1
Edit: So MySQL allows it, but I don't think it's a good practise to use it.
You are trying to query non-aggregated data from an aggregated query. You should not do that.
A GROUP BY takes the field it should make group of rows with (in your case, what you say with your GROUP BY is: give me a result per different group_id) and gives a result (the aggregated data) based on the grouping.
Here, you try to access non aggregated data (rubric_id in your case). For some reason, the query does not crash and picks a "random" id in your aggregated data.
I am trying to understand how Pagination works in mySQL. Considering I have a lot of data that is to be retrieved based on select query how does adding different columns in the select statement change the pagination?
e.g.
Select name from employee; vs Select name, employeeId from employee;
Will using employeeId in the select field help in retrieving data in more efficient manner even though that field in not required. Adding it as employeeId is indexed.
Thanks
Pagination deals with rows, not columns, and is as simple as a LIMIT clause:
LIMIT 10 OFFSET 10
Would give you 10 rows starting from the 10th record.
There is no implicit pagination in MySQL. If you are looking to implement pagination based on your query results, the LIMIT clause may come handy.
For example:
select name from employee limit a,b;
will return b rows from the table employee. These would be row numbers a+1 through a+b
select name from employee 0, 10
would return rows 1 through 10.
Using any number of columns will not affect the way you limit the number of rows which is normally referred to as pagination.
i have a column full of resort id's say 44 rows, i am using the following query
Query-> SELECT DISTINCT RESORT ID FROM Schema.table Name WHERE Condition='Value' AND ROW NUMBER= 1
the above query returns one value say='15'
when i run it multiple it is returning the same value ='15'!!!
i require a different value each time the query is ran
could any one please help me out.
Thanks,
You will get Random Resort ID by using this
SELECT TOP 1 ResortID FROM TableName
-- WHERE <Condition>
ORDER BY NEWID() ASC
Table works(emNo, comNo, salary).
I can get distinct comNo using "select distinct comNo from works". Suppose it gives me a column with 5 rows. How do I count "emNo`" for each of those rows?
You can use GROUP BY to aggregate per type of comNo.
SELECT
comNo,
count(emNo)
FROM
works
GROUP BY
comNo
This will return one row per distinct value of comNo along with the count of records per group.
Demo: http://www.sqlfiddle.com/#!2/4f5df/1
SELECT kullaniciNick,
kullaniciAdi,
kullaniciSoyadi
FROM panelkullanicilari
WHERE id IN
(SELECT user_id
FROM proje_ekip
WHERE proje_id=11)
ORDER BY kullaniciSoyadi
at the query i need the count of rows to check if it is over 6 or less.
When i used the COUNT(*) i got an error message. That said it must used with GROUP BY.
Thank you.
Try this:
SELECT kullaniciNick,
kullaniciAdi,
kullaniciSoyadi,
count(*) -- Added this line
FROM panelkullanicilari
WHERE id IN
(SELECT user_id
FROM proje_ekip
WHERE proje_id=11)
GROUP BY 1,2,3 -- Added this line
ORDER BY kullaniciSoyadi
You can't have a result, and it's size in one query.
Use 2 queries. First one will give you the size of result and second one will be the result. The First query, should be like this:
SELECT count(*)
FROM panelkullanicilari
WHERE id IN
(SELECT user_id
FROM proje_ekip
WHERE proje_id=11)
the second one , is just like the query you wrote above.