I am getting the following error message when executing my query
[2/2] DBALException: An exception occurred while executing 'SELECT DISTINCT s0_.id AS id0, s0_. AS 1 FROM shop s0_ WHERE s0_.isLocked = ? ORDER BY s0_.owner_id DESC LIMIT 20 OFFSET 0' with params [0]:
SQLSTATE[42000]: Syntax error or access violation: 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 'AS 1 FROM shop s0_ WHERE s0_.isLocked = 0 ORDER BY s0_.owner_' at line 1 +
[1/2] PDOException: SQLSTATE[42000]: Syntax error or access violation: 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 'AS 1 FROM shop s0_ WHERE s0_.isLocked = 0 ORDER BY s0_.owner_' at line 1
Is this supposed to be a query error or is this something else ? Because it seems that my query is legit.
SELECT DISTINCT s0_.id AS id0, s0_. AS 1
It is supposed to be a query error. You are selecting the unique id's from table s0_ and a field with no name of that same table, but a field can't have no name.
You should add a fieldname, like this:
SELECT DISTINCT s0_.id AS id0, s0_.FIELDNAME AS `1`
SELECT DISTINCT s0_.id AS id0, s0_. AS `1`
wrap 1 in ``
And the missing field name from comments above...
Related
Im trying to search on Datatable , but getting this error:
Exception Message:↵↵SQLSTATE[42000]: Syntax error or access
violation: 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 'AS fullname like ?) or LOWER(drivers.street_no) LIKE ?
or LOWER(drivers.`s' at line 1
and this what the resultant query
'select count(*) as aggregate from (select '1' as `row_count` from `drivers` where (LOWER(`drivers`.`id`) LIKE %fuj% or LOWER(`drivers`.`licence_no`) LIKE %fuj% or (CONCAT(first_name,' ',last_name) AS fullname like %fuj%) or LOWER(`drivers`.`street_no`) LIKE %fuj% or LOWER(`drivers`.`status`) LIKE %fuj%)) count_row_table'.
Any help would be really appriciated ..
Thanks
I am facing trouble executing following Select Query in Yii framework.
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
error in your SQL syntax; check the manual that corresponds to your
MariaDB server version for the right syntax to use near 'AS
Invoiced_Amount FROM invoice i LEFT JOIN invoice_items ii ON
i.invo' at line 1
The SQL being executed was:
SELECT `i`.`invoice_id`, `i`.`Order_Type`,
`i`.`Trading_Partner_Name`, `i`.`Invoice_Create_Date`,
`i`.`Invoice_Status`, `ii`.`Item_Number`, `ii`.`Invoiced_Qty`,
`ii`.`Item_Rate`, Truncate((ii.Invoiced_Qty * ii.Item_Rate), `2)` AS
`Invoiced_Amount` FROM `invoice` `i` LEFT JOIN `invoice_items` `ii` ON
i.invoice_id = ii.invoice_id WHERE `i`.`Invoice_Status`='17' LIMIT 20
My query is like ..
SELECT oi.`sku`,oi.`product_id`, count(*) as `count1`
FROM `order_item` AS oi RIGHT JOIN `products` AS p ON oi.`product_id` = p.`entity_id` WHERE oi.`product_id` IN (1234,4556,7854)
GROUP BY oi.`sku` ORDER BY `count1` DESC
i got the error like below..
PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 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 ')
GROUP BY sfoi.sku ORDER BY count1 DESC' at line 4'
Please can you guys explain this ,is there any wrong on my query?
You should GROUP BY by two columns: oi.sku,oi.product_id
SELECT oi.sku,oi.product_id, count(*) as count1
FROM order_item AS oi
RIGHT JOIN products AS p ON oi.product_id = p.entity_id
WHERE oi.product_id IN (1234,4556,7854)
GROUP BY oi.sku,oi.product_id
ORDER BY count1 DESC
I have a php pdo script where I want to select the record (from a table in a MySQL database) with the highest number in "field5". I also have a few other constrictions, see below:
$stmt=$db->query("SELECT `field1`,`field2` FROM ".$tablename." WHERE
`field3`!=".$variable1." AND `field3`!=".$variable2." AND
`field4`='xx' AND `field5`<".$variable3." ORDER BY DESC `field5` LIMIT
1");
I have pretty much all the code inside a try-statement, and in the catch statement I use
var_dump($ex->getMessage());
to get the exception message from the exception $ex.
Now, when I execute the code I get the following exception message:
'SQLSTATE[42000]: Syntax error or access violation: 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 'DESC field5
LIMIT 1' at line 3' (length=232)
I'd be grateful for any advice on what might be wrong!
Change
ORDER BY DESC field5
into
ORDER BY field5 DESC
Kindly refer the MySQL manual for the syntax of select query. You have made a simple mistake which is, the query was syntactically wrong. You can only order the column by referencing it initially :
ORDER BY '{Column-Name}'
and then only you can define how it can be ordered either in ASC or DESC.
ORDER BY '{Column-Name}' [ASC|DESC]
So you have to change the query as shown here :
$stmt=$db->query("SELECT `field1`,`field2` FROM ".$tablename." WHERE
`field3`!=".$variable1." AND `field3`!=".$variable2." AND
`field4`='xx' AND `field5`<".$variable3." ORDER BY `field5` DESC
LIMIT 1");
I'm getting this error:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AS `Colleges__*` FROM college_admins CollegeAdmins LEFT JOIN colleges Colleges O' at line 1
Here is the SQL query which is giving this error:
SELECT Colleges.* AS `Colleges__*` FROM college_admins CollegeAdmins LEFT JOIN colleges Colleges ON Colleges.id = (CollegeAdmins.college_id) WHERE CollegeAdmins.user_id = :c0 LIMIT 20 OFFSET 0
I enabled quoteIdentifiers config\app, but it leads to this new error:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AS `Colleges__*` FROM `college_admins` `CollegeAdmins` LEFT JOIN `colleges` `Col' at line 1
where the query becomes:
SELECT `Colleges`.* AS `Colleges__*` FROM `college_admins` `CollegeAdmins` LEFT JOIN `colleges` `Colleges` ON `Colleges`.`id` = (`CollegeAdmins`.`college_id`) WHERE `CollegeAdmins`.`user_id` = :c0 LIMIT 20 OFFSET 0
I think it's taking the 'Col from Colleges as the keyword 'COL', but I'm not sure. How to fix this?
This is the CakePHP code which is generating the MySQL query:
return $college_admins->find()
->select(['Colleges.*'])
->leftJoinWith('Colleges')
->where(['CollegeAdmins.user_id' => $userId]);
You cannot use Colleges.* in a CakePHP ORM query (CakePHP 3.x). As you've discovered this creates incorrect SQL aliases like Colleges__*. Instead to select all columns of a table you need to pass a table object.
So you'd probably be wanting to do something like:-
->select($college_admins->Colleges)
Assuming Colleges is associated with your CollegeAdmins table.
You cannot alias colleges.*, since this refers to all columns within colleges table and aliases refer to a single column (or table or subquery). You need to list all fields within the colleges table and provide an alias for each of them, such as
select colleges.ig as colleges_id, colleges.field1 as colleges_field1, ...
There is not syntax in sql to provide alias such way. What you may try to do is to access the metadata returned by mysql in php to retrieve the table name for each field.