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?
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 been working on a SQL query for a project, and I face an error message when I want to use it.
Here is the query itself :
SELECT COUNT(r) AS auditMade,
SUM(g.nbrMilkingCows) AS cowsAudited,
AVG(r.gainPerCowPerYearTransition) AS averageGainTransition,
AVG(r.gainPerCowPerYearLactation) AS averageGainLactation,
AVG(r.totalGain) AS averageTotalGain,
AVG(r.supplementalCostPerCow) AS averageSuppCost
FROM `smart_calculator_infos` i
INNER JOIN `smart_calculator_result` r ON r.idSmartCalculatorResult = i.idSmartCalculatorResult
INNER JOIN `calculator_general_informations` g ON g.idSmartCalculatorInfo = i.idSmartCalculatorInfo
WHERE i.idUser = 14
MySQL answers me "Unknown column 'r' in field list".
But I dont really understand why I get an error here as I define r in my INNER JOIN.
I'm kinda new at using SQL so maybe there is something pretty obvious I forgot, but I can't seem to understand what.
You can't count an alias itself, so the very first line of your query is what is causing the error:
SELECT COUNT(r)
To remedy this, you could use COUNT(*):
SELECT COUNT(*)
Or, you could count an actual column in the smart_calculator_result table, e.g.
SELECT COUNT(r.idSmartCalculatorResult)
I need to write a little tricky subquery in where clause
So, I have a query like this
Select * FROM table1
JOIN table2 ...
LEFT JOIN table3 ...
WHERE (SELECT COUNT(*) FROM some_table where some_condition) = 0;
The SQL works fine, but how can I move it into the Laravel?
Yeah, I know that I should use DB::raw() but the problem is the subquery returns count. So, for example, the count is 5454. After that, I'm having something like this.
->where(5454, '=', 0)
and it gives me obvious error message: Unknown column 5454 ...
I've tried also to use AS count for the subquery but in that case, I'm having another obvious error message: Syntax error 0_0
So, any suggestions?
Try
whereRaw("(SELECT COUNT(*) FROM some_table where some_condition) = 0")
This will help you.
I am new to MYSQL and I was trying to do an inner join but one of the columns is named TS%. It seems that the symbol at the end is causing this problem because when I remove this column it works just fine
SELECT Players.Player, Players.college, Seasons_Stats.Year, Seasons_Stats.Pos, Seasons_Stats.TS%
FROM Players
INNER JOIN Seasons_Stats on Players.Player = Seasons_Stats.Player;
Use backquote should resolve that strange column name :
SELECT Players.Player, Players.college, Seasons_Stats.Year, Seasons_Stats.Pos,
Seasons_Stats.`TS%` FROM Players INNER JOIN Seasons_Stats on Players.Player = Seasons_Stats.Player;
Because the % symbol in a MySQL syntax is used for some query operation.
EDIT: I am using phpMyAdmin interface, and I have been copy/paste the codes from phpMyAdmin to here. The phpMyAdmin seems to run a "different code" as I run the following code, and generating some error message that are referring to that "different code", causing huge confusion.
** Final edit: It seems Safari is causing this: it run the "different query" when I try to run 2nd query below. Use Firefox instead, and it generate correct results. Thanks for the help and sorry for the confusion. **
I have two tables: newsFeeds, comments, where
** newsFeeds contains column PID, comments contains column FID.**
I want to join rows in two tables with matching PID = FID. My code is:
SELECT * FROM newsFeeds
INNER JOIN
(
SELECT * FROM
comments
)
ON comments.FID = newsFeeds.PID
and the error message is "#1248 - Every derived table must have its own alias".
I then add in AS newtb after ) according to other posts here. And the code is then:
SELECT * FROM newsFeeds
INNER JOIN
(
SELECT * FROM
comments
) AS newtb
ON newtb.FID = newsFeeds.PID
But another error shows up: #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 'INNER JOIN( SELECTFID,COUNT(*) AScount FROMcomments
LIMIT 0, 25' at line 8
I wonder how to correctly do this?
You should correct this by removing the derived table:
SELECT *
FROM tb_1 INNER JOIN
tb_2
ON tb_2.FID = tb_1.PID;
MySQL has a tendency to materialize derived tables, which hurts performance.
The answer to your question, though, is to add a name after the parentheses:
SELECT *
FROM tb_1 INNER JOIN
(SELECT *
FROM tb_2
) t2
ON t2.FID = tb_1.PID;