I use group by with a count function in LARAVEL 4.2. When I tried to use LIKE in the same query, it gave me the error Invalid use of group function, as we can't use like with group by. How can I solve this?
You can't use Mysql aggregate function with group & like both together you have to use having instead of like.
Check here MySQL :: MySQL 5.0 Reference Manual :: 12.16.3 MySQL Handling of GROUP BY
and here: A similar SO question answer
Related
I just deployed my app and had to switch from mysql to postgresql.
Now when executing my query I get following error:
ActiveRecord::StatementInvalid (PG::GroupingError: ERROR: column "search_terms.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT "search_terms".* FROM "search_terms" WHERE (search_te...
^
: SELECT "search_terms".* FROM "search_terms" WHERE (search_terms.term like '%%') GROUP BY search_terms.game_id):
I have a table called search_terms with term:string and game_id:integer. A game can have multiple terms. So I want to group the terms that match by the game_id.
Does anyone know how to solve this?
In my model search_term.rb, I have defined this search function:
# Search Terms
def self.search(query)
where("search_terms.term like ?", "%#{query}%").group("search_terms.game_id")
end
Thank you for any help.
I think this is what you need to try.
def self.search(query)
where("search_terms.term like ?", "%#{query}%").group("search_terms.game_id", "search_terms.term")
end
explanation here: Rails 3.1 with PostgreSQL: GROUP BY must be used in an aggregate function
In TableAdapter Configuration Wizard, I am having an issue with my SQL statement.
I wanted to filter CreatedDate for that particular Year based on the current date. So I use the Year() function, but when executed, it prompts an error message.
Below is my SQL statement.
SELECT cff.CFNo, cff.RegionalOffice, cffitemstatus.Reason
FROM cff, cffitemstatus
WHERE cff.RecordNo = cffitemstatus.RecordNo
AND (cff.CreatedDate = `YEAR`(CURDATE()))
ORDER BY cff.RegionalOffice, cffitemstatus.Reason
Need guide and advise on this issue.
You should learn to use explicit JOIN syntax. Simple rule: Never use commas in the FROM clause.
Then, one method to do what you want is to use YEAR() on both dates:
SELECT cff.CFNo, cff.RegionalOffice, cffitemstatus.Reason
FROM cff JOIN
cffitemstatus
ON cff.RecordNo = cffitemstatus.RecordNo
WHERE YEAR(cff.CreatedDate) = YEAR(CURDATE())
ORDER BY cff.RegionalOffice, cffitemstatus.Reason;
I need to run a regex find-and-replace against a column named message in a MySQL table named post.
My database is running MariaDB 10.
According to the docs, MariaDB 10 has a new REGEXP_REPLACE function designed to do exactly this, but I can't seem to figure out the actual syntax.
It will affect 280,000 rows, so ideally there's also a way to limit it to only changing one specific row at a time while I'm testing it, or simply doing a SELECT rather than an UPDATE until I'm sure it does what I want.
The regex I want to run:
\[quote\sauthor=(.+)\slink=[^\]]+]
The replacement string:
[quote="$1"]
The following was what I tried, but it just throws a SQL error:
UPDATE post SET message = REGEXP_REPLACE(message, '\[quote\sauthor=(.+)\slink=[^\]]+]', '[quote="$1"]') WHERE post_id = 12
In this case, the original message was:
[quote author=Jon_doe link=board=2;threadid=125;start=40#msg1206 date=1065088] and the end result should be [quote="Jon_doe"]
What is the proper syntax to make this REGEXP_REPLACE work?
You have to do a lot of escaping here:
REGEXP_REPLACE(message, "\\[quote\\sauthor=(.+)\\slink=[^\\]]+]", "\\[quote=\"\\1\"\\]")
Please note that you have to reference the Group by \\1
I have a string returned from a function "'aa','bb','cc',..."(the function uses GROUP_CONCAT). I want to use this as a condition in the IN clase of mysql.
SELECT name,class,comment
FROM vwstudent v
WHERE name IN (select func())
I want the query to act like
SELECT name,class,comment
FROM vwstudent v
WHERE name IN ('aa','bb','cc','dd',..)
I assume ('aa','bb','cc','dd',..) is acting as a whole string here and isn't generating any result. What can I do to run this query error less.
I'm not sure if it'll help. But you might be able to hack something together by using PREPARE and EXECUTE as described here: How To have Dynamic SQL in MySQL Stored Procedure
... but that's completely new to me. I've never used anything like that in MySQL.
I have a sql query like this
select column from table where path = left('INPUTSTRING', length(path));
and trying to accomplish it in hql like this,
return session.createQuery("from Table where Path = left(:input, length(Path))").
query.setParameter("input", inputPath).
.list();
and getting an error like this
Caused by: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: left near line 1
how to get this done? What is the corresponding string function in hql? Is there a solution for this using criteria query apis?
Yes, left() is not supported by the MySQLDialect. See the list of HQL supported functions on API docs.
Now you have left with 2 options.
Use session.createSQLQuery() method.
Create Your own Dialect class by extending the MySQLDialect and register the function there. This is told at hibernate forum here explained well in a blog post here.
I'm not sure if HQL does this for you , but you can use IQuery/session.CreateSQLQuery() to use a raw SQL query to populate a mapped entity. I've never used it for substrings, but have used it for aggregate functions. Check chapter 13 of the NHibernate docs and see if that does it for you. You can check the query substitution available in Nhibernate - here