I'm trying to run this query
$tableDate = DB::select('SELECT (DATE_FORMAT(created_at,"%m-%d-%Y")) as
dateTaken FROM `test_answers`WHERE user_id= '.$id.' GROUP BY (test_id)
');
but it is giving me this error
SQLSTATE[42000]: Syntax error or access violation: 1055 'mte30.test_answers.created_at' isn't in GROUP BY (SQL: SELECT (DATE_FORMAT(created_at,"%m-%d-%Y")) as dateTaken FROM test_answers WHERE user_id= 2 GROUP BY (test_id) )
I tried it on PHPMyAdmin and it worked.
Can someone tell me why it's not working using Eloquent?
Thank you
The reason this doesn't work with strict mode on is because you haven't specified in the query which created_at to use. If you're using GROUP BY and you are selecting a column that isn't grouped on or aggregated, you may not receive the correct data.
You have multiple rows in test_answers but MySQL doesn't know which row's created_at you care about. You could fix this by simply telling MySQL which created_at to return, something like MAX(created_at) to select the most recent, or MIN(created_at) to select the oldest.
Related
I am using laravel framework for developing API's ,i have one query that is executed without where condition without any error i need to execute with where condition but it's throwing an error
query
select count(*) as aggregate
from `users`
left join `books` on `books`.`book_id` = `books`.`id`
where `access_id` = 5054
SQL Error [1052] [23000]: Column 'access_id' in where clause is
ambiguous
after searching google i got something we have to specify reference of a table name , i added reference name like this where users.access_id =5054 but it's throwing an error like unknown column but in my db i have that column in both users and books table
The problem is its consider as a column so that's why syntax error is coming,try following way it will resolve your problem
select count(*) as aggregate
from `users`
left join `books` on `books`.`book_id` = `books`.`id`
where `users`.`access_id` = 5054
I've managed to get the first part of my query working fine, it's the final part with the date check that is throwing me.
SELECT * FROM users WHERE available LIKE 'Yes' AND users.id NOT IN (SELECT player_id FROM match_request WHERE club_id LIKE '1000000003') AND (clubs.match_date >= CURRENT_DATE() WHERE clubs.id LIKE '1000000003')
It's this part that I can't work out:
(clubs.match_date >= CURRENT_DATE() WHERE clubs.id LIKE '1000000003')
I get the error message
"#1064 - You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'WHERE clubs.id LIKE '1000000003') LIMIT 0, 25' at line 1"
But I don't know how to fix this.
Replace where with and
SELECT *
FROM users
WHERE available LIKE 'Yes'
AND users.id NOT IN
(
SELECT player_id
FROM match_request
WHERE club_id LIKE '1000000003'
)
AND clubs.match_date >= CURRENT_DATE()
AND clubs.id LIKE '1000000003'
^^^-----here
and you need to join the clubs table if you refer to any columns of that table
I found a few other threads with this error message on the site but the solutions there did not seem to work for me.
This is the query I am trying to run:
SELECT
o.name as Name,
o.vrank_tav__c as Vrank,
COUNT(c.enterprise_id) AS #_users_enterprise
FROM
(community_csv_james c JOIN
salesforce_data_opportunity o ON
c.enterprise_id = o.enterprise_id__c)
GROUP BY #_users_enterprise, Name, Vrank
ORDER BY #_users_enterprise DESC;
When I run it on SQL Workbench J, I get the following error:
SELECT
o.name as Name,
o.vrank_tav__c as Vrank,
COUNT(c.enterprise_id) AS #_users_enterprise
FROM
(community_csv_james c JOIN
salesforce_data...
ERROR: aggregates not allowed in GROUP BY clause
I've tried a few variations of this but I that promoted different error messages. How should I write this query?
Thanks!
You are not supposed to include the results from your aggregate function (your Count()) in your group by. The count is going to be associated with a distinct name/Vrank so you would only need to group on those. That's why it's giving you that specific error.
GROUP BY Name, Vrank
MySQL documentation for GROUP BY
i have a query that fetches data's from differant tables
SELECT p.USER_NAME,count(*) as total, pi.UpdatedDate FROM purchase p JOIN purchasedissues pi on pi.PurchaseId=p.PURCHASE_ID WHERE pi.UpdatedDate>DATE_SUB(NOW(), INTERVAL 1 HOUR) AND p.PURCHASE_DATE=CURDATE() AND p.USER_NAME NOT IN (SELECT username from tbl_test_user) GROUP BY p.USER_NAME having count(*)>2
but i got an error that
#1064 - You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'SELECT p.USER_NAME,count(*) as total, pi.UpdatedDate FROM
purchase p JOIN pur' at line 1
i can't figure it. please any one help me
thanks in advance
Your query is ok. The error is indicating that it is near your SELECT p.USER_NAME,.... This usually happens when you are executing multiple query without terminating it. Ex,
SELECT * FROM tableName
SELECT p.USER_NAME,count(*) as total, pi.UpdatedDate FROM purchase....
so to correct it, just add a delimiter (usually if not change, it's semi-colon) to end your first query
SELECT * FROM tableName; -- <== this one
SELECT p.USER_NAME,count(*) as total, pi.UpdatedDate FROM purchase....
Try this query! not sure if it works as I don't have the environment set in my machine but it may give you an essence to test it :)
SELECT p.USER_NAME,count(*) as total FROM purchase p JOIN purchasedissues pi on pi.PurchaseId=p.PURCHASE_ID WHERE pi.UpdatedDate>DATE_SUB(NOW(), INTERVAL 1 HOUR) AND p.PURCHASE_DATE=CURDATE() AND p.USER_NAME NOT IN (SELECT username from tbl_test_user) GROUP BY p.USER_NAME having total > 2
I'm having an issue getting a COUNT() from a SQL query using Zend_Db_Table_Select, and I think it may be a possible bug because the SQL it should be generating actually works. Here's the Zend Select Query: ($this is a Zend_Db_Table, renamed to table1 in this example)
$select = $this->select();
$select->setIntegrityCheck(false);
// Select Count
$select->from($this, array("COUNT(*) as 'COUNT'"))
->joinLeft('users', 'table1.userID = users.userID')
->joinLeft('table2', 'users.anotherKey = table2.anotherKey');
// Add Where clause after join
$select->where('users.anotherKey = ?', $anotherKeyValue);
This gives the error:
SQLSTATE[42000]: Syntax error or access violation: 1140
Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is
illegal if there is no GROUP BY clause`
However, this query...
SELECT COUNT(*) AS 'count' FROM table1
LEFT JOIN users ON table1.userID = users.userID
LEFT JOIN table2 ON users.anotherKey = table2.anotherKey
WHERE users.anotherKey = [anotherKeyValue]
...returns the expected results with no errors when run against the database. Any ideas whats going on, why the error, and how to get around it?
have you tried to see actual query, that zend_db produce?