This question already has answers here:
How do I select all the columns from a table, plus additional columns like ROWNUM?
(4 answers)
Closed 5 years ago.
Some SQL toolsets like PDO can do special things based on the first column selected, such as take it as class name to instantiate or to use it as key into a hashtable. Unfortunately PDO removes that column from the result. What if I still want it to be part of the result?
I've tried queries such as
SELECT `class` as `myclass`, * FROM `mytable`
but I'm getting errors:
#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 '* FROM mytable at line 1
I understand that there can't be conflicts in column names, hence the
as `myclass`
And the following works just fine:
SELECT `class` as `myclass`, `class` FROM `mytable`
Is this possible at all without doing a self-join or putting the full list of columns?
You can do like below
SELECT `mytable`.`class` as `myclass`, `mytable`.* FROM `mytable`
or like this
SELECT t.class AS myclass, t.* FROM mytable t;
Related
This question already has answers here:
How to delete from multiple tables in MySQL?
(7 answers)
Closed 2 years ago.
This code does not work. Without creating foreign key between tables how can I delete data from multiple tables that matches the conditions? How can I write a 2 query and send it with PHP-PDO
$query = "DELETE FROM category, bookmark WHERE (bookmark.category = ? AND category.name = ?)";
$stmt = $db->prepare($query);
$stmt->execute([$categoryName, $categoryName]);
I am using "?" to prevent from sql injection.
This is the error I get.
JavaSQLSTATE[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 'WHERE
(bookmark.category = 'Java' AND category.name = 'Java')' at line 1
In multi-table DELETE you MUST specify the tables which records must be deleted:
DELETE category, bookmark
FROM category, bookmark
WHERE (bookmark.category = ? AND category.name = ?)
See MySQL 8.0 Reference Manual / ... / DELETE Statement, section "Multiple-Table Syntax". The names of tables which records must be deleted are NOT enclosed into square brackets as optonal (2nd row of query text in both syntax variants).
This question already has answers here:
PHP, MySQL error: Column count doesn't match value count at row 1
(3 answers)
Closed 6 years ago.
$sql="INSERT INTO `tempahan`(`ic`,`nama`,`tarikh`,`tarikhakhir`,`mula`,`akhir`,`unit`,`bil`,`sebab`) ``SELECT ic, nama
FROM register";`
i use this to select the register column to be inserted in tempahan column.
but it gives me error 'Column count doesn't match value count at row 1'
just insert into the columns you really want to insert into - as the error states, the number of the columns need to be identical:
$sql="INSERT INTO `tempahan`(`ic`,`nama`) (SELECT `ic`, `nama`
FROM `register`)";
update: you can't use VALUES() when using a subquery to get the insert-values. I just corrected this.
Your insert statement for tempahan table provides more columns than you select from register table. Error message clearly says this. Use this:
$sql="INSERT INTO `tempahan`(`ic`,`nama`) ``SELECT ic, nama FROM register";`
I am relatively new to somewhat advanced MySQL querying. I had been trying to query the most recent order in an order table of a particular user using MySQL SELECT statement using the following MySQL query.
SELECT o1.* FROM order AS o1
WHERE o1.orderDateTime =
(
SELECT MAX(o2.orderDateTime) FROM order AS o2
WHERE o2.userId = '1'
)
But I had been constantly getting the following MySQL error #1064 related to MySQL syntax.
#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 'order AS o1 WHERE o1.orderDateTime = (SELECT MAX(o2.orderDateTime)FROM order AS ' at line 1
I got similar errors in relation with INSERT statements but I managed to fix it up using the methods specified in MySQL 1064: You have an error in your SQL syntax
I made every effort to fix the query in the current case but I was still unsuccessful.
I would be grateful to you if someone can help me out with fixing this MySQL syntax error for SELECT clause specified above. It would be great if someone could specify me the exact reason for the occurrence of this issue, as well.
order is a reserved word and its a bad choice for table name. You need to escape using backticks in the query
SELECT o1.* FROM `order` AS o1
WHERE o1.orderDateTime = (
SELECT MAX(o2.orderDateTime) FROM `order` AS o2
WHERE o2.userId = '1'
)
http://dev.mysql.com/doc/mysqld-version-reference/en/mysqld-version-reference-reservedwords-5-5.html
As per #Abhik, order is a MySQL keyword.
And you should avoid collapse with two methods:
Use backticks (`) (#Abhik has already explained this.)
Prepend Database name before Table Name e.g.
DataBase_Name.order.
But, still #Abhik's approach is preferable as in case of database name change, you need to change DataBase name in your query.
First of all you could follow #Abhik Chakraborty suggestion to include back ticks around order table name. order is a reserved word in mysql. My suggestion was to improve your sql query. YOu could acomplish the same using:
SELECT o1.* FROM `order` o1
WHERE o1.userId = '1' order by orderDateTime desc limit 1
the subquery seems unnecessary.
This question already has answers here:
MySQL Error 1093 - Can't specify target table for update in FROM clause
(16 answers)
Closed 8 years ago.
I have a table in database where I am trying to update some data.
Table1
Uname Password Email SpecialID
A qwh2 abc#xyz.com 23243
B rt4f aafj#xyz.com 56343
I am trying to change the SpecialID from this query:
Update Table1 SET SpecialID='24152' where Uname=(select Uname from Table1 where Email='abc#xyz.com');
But I am getting this error :
ERROR 1093 (HY000): You can't specify target table 'Table1' for update in FROM clause
Please help me and tell me what is wrong I am doing in this query...!!! I searched but didn't get proper solution..
Use join instead,in mysql you can't use same table in update/delete in sub queries.
Update Table1 t
join Table1 t1 on(t.Uname = t1.Uname)
SET SpecialID='24152'
where t1.Email='abc#xyz.com'
Yo only need to change the condition:
Update Table1 SET SpecialID='24152' where Email='abc#xyz.com';
I hope it works fine for you.
This question already has answers here:
How do I escape reserved words used as column names? MySQL/Create Table
(4 answers)
Closed 9 years ago.
I'm running this on my website:
$PlaceOrder = " Insert INTO Order VALUES ( '$CustomerID' , '$ItemID' , '$Quantity' , '$Date' ) ";
$result = mysql_query ($PlaceOrder);
if (!$result)
{
die('Invalid query: ' . mysql_error());
}
But when I ever I do I keep getting the following error message:
Invalid query: 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 'Order VALUES ( '3' , '1' , '12' , '11/05/2013' )' at line 1
I really have no idea what to do now, I tried specifying the columns but that didn't work either. I am very new to this whole thing and I'm executing this based on whats in the manual.
Let's look at your error message again:
[...] the right syntax to use near'OrderVALUES
The first thing it complains about is Order. That's what you substitute the Table from your question for. So let's assume that's your actual table name.
It's also a reserved word. Enclose reserved words in backticks when used as column or table name identifiers.
Order is Key word Just use another word instead of Order
Are you sure there are only 4 columns and are they in the same order as you have specified in your query?
As you suggested try to add the column names, for example:
$PlaceOrder = "INSERT INTO Table (customer, item, quantity, date) VALUES ('$CustomerID', '$ItemID', '$Quantity', '$Date')";
Does this help you?
First of all, you should always specify the columns since you never know when you will have to add new columns to the table and old queries will start doing messy things if you haven't done that.
Having said that, are you sure those are the correct column data types that match the table's?
Also, make sure the date format is valid, you shoud use Y-m-d. You will find more info here.