I've been googling around and can't seem to find how to fix this. I'm currently using MySQL and am trying to run this
SELECT song_id FROM ‘played_songs’ WHERE id=MAX(id)
When I run this, I get the error message
1111 - Invalid use of group function
Does anyone know what I am doing wrong here? For reference, I'm trying to get the latest row's song_id in a pre-existing database. The ID increments so the latest value is the max ID in the column. When I get that column, I would like to get it's song_id in that same row.
Sorry if this is a super basic question, I've never done this before.
Thanks!
you can't use max in where clause, try this:
SELECT song_id FROM played_songs WHERE id=(SELECT max(id) FROM played_songs)
but better solution is:
SELECT song_id FROM played_songs ORDER BY id DESC LIMIT 1;
You need to use a "Having" instead of a "where". A Having is used when you are operating on a previous calculation (getting max id is a calculation mysql has to make). I would write like this.
SELECT song_id, MAX(id) as
max_id FROM ‘played_songs’
HAVING max_id=id
Related
Each time a php script runs i want to delete old entries by id. But MySQL throws me this error:
#1111 - Invalid use of group function
This is the query:
DELETE FROM am_shoutbox WHERE MAX(msg_id)-160 > msg_id
What is the problem here? I tried around and solved it by selecting the highest id to php first and then delete with a second query, but for better performance i want to do this in one if possible.
I hope someone can figure out what is wrong with the query above.
You can't use aggregate functions in the WHERE clause. You can try something like this (using subquery that is retrieving MAX(msg_id)) :
DELETE FROM am_shoutbox
WHERE ( SELECT *
FROM ( SELECT MAX(msg_id)
FROM am_shoutbox ) m ) - 160 > msg_id
The invalid use of group function the error describes is due use of MAX(msg_id) in WHERE clause. You can use it either in a select/subselect or in the HAVING clause:
delete from am_shoutbox
where
(select (max(ams.msg_id) FROM am_shoutbox ams) - 160) > msg_id
*you may need to specify a table alias in the sub-query as above
or the more elegant and better performance way:
delete from am_shoutbox
having (max(msg_id) - 160) > msg_id
I'm new to Stackoverflow, and I'm pretty new to MSSQL too (I've been using MYSQL for a couple of years now), and I was wondering if anyone could please help me with a query I'm having trouble with?
I've made a story archive page for a site that I'm putting together, and it's working fine, but the only problem is that I want to archive everything except for the top 6 stories in my table.
I came across a piece of code which will ignore the top few results (6 in my case), but I'm a little bit stumped as to how to incorporate it into my query.
I've tried a few things but I keep getting error messages - I think I'm way off track.
Any help that anyone can give me would be hugely appreciated!
Thank you very much in advance:)
This is the query I found:
SELECT *
FROM PageContent
WHERE id NOT IN (
SELECT TOP 6 id
FROM PageContent
ORDER BY date)
This is my query:
SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (ORDER by PageId desc) AS RowNum
FROM PageContent WHERE pagestory_type = 'latest_news'
) AS MyDerivedTable
WHERE MyDerivedTable.RowNum BETWEEN $qpage_srt and $qpage_fin ORDER by date desc";
If you have SQL Server 2012 you can try:
SELECT *
FROM PageContent
ORDER BY [date]
OFFSET 6 ROWS
Why can't you just use the TOP clause with a select from the table ordered by the column you need? Similar to the sample you found.
SELECT TOP X * -- add fields as needed
FROM PageContent
WHERE pagestory_type = 'latest_news'
ORDER BY PageId DESC
i dont find any difference in your query except the parameter variables
$qpage_srt and $qpage_fin
it should be #qpage_srt and #qpage_fin in Ms sql...
I would like get the id and the number of id. So, I write this command sql:
SELECT count(id), id
FROM tblExample
It's doesn't work. Have you a solution for me ? For to get the value of my id and the number of id.
Or a function PHP for count my resultset.
Just add a GROUP BY id:
SELECT id, COUNT(id)
FROM tblExample
GROUP BY id;
Demo
Update:
The query you posted:
SELECT count(id), id
FROM tblExample;
Won't work in most of the RDBMS and it shouldn't. In SQL Server, you will got an error; saying that:
Column 'id' is invalid in the select list because it is not contained in either an
aggregate function or the GROUP BY clause.
Strangely though, MySQL allow this(The OP didn't say what RDBMS he is using), and in this case, it will get an arbitrary value (this is also depends on an option to set), for the id column, and the COUNT in this case would be all the id's count.
But it is not recommended to do so.
I have a problem in finding latest data from the database.
When I select no from book is returned from 1,2,3.
But when I do this, supposedly think it should return the value 3 this returns me 2.
SELECT no FROM book WHERE rowid =( SELECT MAX(rowid) FROM book)
The strange this is that if you delete all data from the list, it starts me back the last line as well, until it has more than 2 data. Someone can help me?
It looks like your query is looking for the max of rowid, and not no.
Therefore, try this:
SELECT MAX(no) FROM book;
Shouldn't you be using max(no) in your subquery?
SELECT no FROM book WHERE rowid =( SELECT MAX(no) FROM book)
RowID is a psuedocolumn and starts at 0 - see here.
Try over this
select no from book where rowid = (select max(no) from book)
Why not just SELECT MAX(no) FROM book?
I decided to use favs (id's of users which marked that post as a favorite) as a comma separated list in a favs column which is also in messages table with sender,url,content etc..
But when I try to count those rows with a query like:
select count(id)
from messages
where favs like '%userid%'
of course it returns a wrong result because all id's may be a part of another's
For example while querying for id=1 it also increase the counter for any other content that is favorited by user id 11...
Can you please tell me your idea or any solution to make this system work?
With a few or's, you can have an ugly solution:
select count(id) from messages where favs like 'userid,%' or favs like '%,userid,%' or favs like '%,userid'
There's likely a more elegant solution, but that one will at least return the result you're looking for, I believe.
Is it possible to change your data model such that the association between users and their favorite messages is instead stored in another table?
Storing the associations in a single column negates the advantages of a relational database. You pay a performance cost using the like function, you can no longer store additional data about the relationship, and the data is harder to query.
An alternative model might looking something like this (can't include an image since I'm a new user, but I made one here):
users
- id
messages
- id
favorite_messages
- user_id (foreign key to users.id)
- message_id (foreign key to messages.id)
With that in place, your original query would be simplified to the following:
select count(1) from favorite_messages where user_id = userid
Additionally, you can do things like get a list of a user's favorite messages:
select
*
from
messages
inner join favorite_messages
on messages.id = favorite_messages.message_id
where
user_id = userid
should using this :
SELECT count(id) FROM messages WHERE FIND_IN_SET('userid',favs) > 0
You might have to get the value, explode it with PHP and then count the array.
There are ways to do it in MySQL, but from what I've seen, they are a hassle.