MySQL Distinct Query Error - php

SELECT DISTINCT(reserve_dummy.isbn) as reserved_dummy.isbn,
reserved_dummy.price,
reserved_dummy.qty,
reserved_dummy.date,
reserved_dummy.total
FROM reserved_dummy
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
'.isbn,reserved_dummy.price,reserved_dummy.qty,reserved_dummy.date,reserved_dummy'
at line 1
SELECT DISTINCT(reserve_dummy.isbn) as reserved_dummy.isbn,
reserved_dummy.price,
reserved_dummy.qty,
reserved_dummy.date,
reserved_dummy.total
FROM reserved_dummy
kindly help me figure out my wrong here. thanks in advance

Just quit that strange idea of prefixing every field name with table name
SELECT DISTINCT(isbn) as isbn,price,qty,date,total FROM reserved_dummy

The error is caused by the table prefix when using the SELECT DISTINCT(x.y) AS x.y .... syntax, it should be SELECT DISTINCT(x.y) AS y ....
Take this out and the query should work:
SELECT
DISTINCT(reserve_dummy.isbn) as isbn,
reserved_dummy.price,
reserved_dummy.qty,
reserved_dummy.date,
reserved_dummy.total
FROM
reserved_dummy

SELECT DISTINCT(isbn) AS isbn,
price,qty,
date,total
FROM reserved_dummy
The syntax for the SQL DISTINCT clause is:
SELECT DISTINCT columns FROM tables
Don't use . (dot) operator when aliasing

You cannot do such a query. You can do a
select distinct isbn, price, qty, date, total from reserved_dummy
and you'll get the distinct records.
However, I don't see what do you want to get from this query.
Maybe isbn is the primary key, in that case:
select `isbn`, `price`, `qty`, `date` from reserved_dummy
should work.
Note the backquote sign around fieldnames. That's because "date" is a function name in MySQL and using it as fieldname without the quotes will fail.
If it happens that there're several records with the same isbn, and you want to get the data from the newest one you could do a query like:
select `isbn`, `price`, `qty`, `date`
from reserved_dummy
where (`isbn`, `date`) in (
select isbn, max(`date`) from reserved_dummy group by isbn
)
to get what you're looking for.

Related

copy mysql table into another table (within the same database) and auto increment inserted rows

I have two tables listed below:
Table: "wp_topic"
Table: "wp_default_topics"
What i am trying to do is copy all of the rows from wp_default_topics (except for ID) to wp_topic auto-increment from the new table (wp_default_topics).
I can use the below code to copy everything fine:
INSERT INTO wp_topic SELECT * FROM wp_default_topics
but i will end up wit the error "#1062 - Duplicate entry '28' for key 'PRIMARY'"
I have tried:
INSERT INTO wp_topic SELECT * FROM wp_default_topics ON DUPLICATE KEY UPDATE ID=VALUES(ID+1)
But end up with the error message:
"#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 '+1)' at line 1"
and i have tried:
INSERT INTO wp_topic SELECT * FROM wp_default_topics ON DUPLICATE KEY UPDATE LAST_INSERT_ID(wp_topic.ID)
But i end up with the error message:
"#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 '(wp_topic.ID)' at line 1"
I have also tried the below which i thought would work but unfortunately it does not.
INSERT INTO wp_topic SELECT (user_id, name, subject, company, date) FROM wp_default_topics
"#1241 - Operand should contain 1 column(s)"
I have tried a few variations of the above but without any luck could i get a pointer as to what i am missing here?
Thank You
David
You need to skip the id column when inserting, so you have to list all the columns explicitly, not use SELECT *
INSERT INTO wp_topic (user_id, name, subject, company, date)
SELECT user_id, name, subject, company, date
FROM wp_default_topics
Can't you just remove the id field from the select and insert?
INSERT INTO wp_topic (`user_id`, `name`, `subject`, `company`, `date`)
SELECT `user_id`, `name`, `subject`, `company`, `date` FROM wp_default_topics
Another slightly simpler way of writing the INSERT sub-select query that's been suggested already:
INSERT INTO wp_topic
SELECT NULL, user_id, name, subject, company, date FROM wp_default_topics;

Error with not exists code - mariaDB

I'm not sure why:
INSERT INTO $db.further_assess (taskid) VALUES ('id')
WHERE NOT EXISTS (SELECT * FROM $db.further_assess where taskid='$id')
is giving me this error:
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 'WHERE NOT
EXISTS (SELECT 1 FROM risk_assessment.further_assess where taskid='222' at line 2
was following this: Sql insert if row does not exist
Update:
My, now correct, query:
INSERT INTO $db.further_assess (taskid, reportid)
SELECT '$id', '$report_id'
FROM (SELECT 1) as dummytable
WHERE NOT EXISTS (SELECT * FROM $db.further_assess where taskid='$id');
An INSERT statement has no WHERE clause. If you want to run a conditional INSERT statement you can use an INSERT-SELECT statement with a dummy table:
INSERT INTO $db.further_assess (taskid)
SELECT 'id'
FROM (SELECT 1) as dummytable
WHERE NOT EXISTS (SELECT * FROM $db.further_assess where taskid='$id')
Much simpler (and faster) to do
INSERT IGNORE INTO $db.further_assess
(taskid)
VALUES
('$id')

MySQL reports syntax error, but I do not see it?

I'm trying to run this query:
INSERT INTO table_a (fb_uid, from, to, time) VALUES (12345,'blah','test','2012-12-13 11:30:00')
But I'm getting:
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, to, time) VALUES (12345,'blah','test','2012-12-13 11:3' at line 1
The query seems fine to me, what is wrong with it?
Use backticks on your fields to prevent a conflict with MySQL reserved words:
INSERT INTO table_a (`fb_uid`, `from`, `to`, `time`) VALUES (12345,'blah','test','2012-12-13 11:30:00')
In this case, from and to are the reserved words
See here for more information and a complete list of reserved words.
FROM and TO are reserved keyword,
INSERT INTO table_a (fb_uid, `from`, `to`, time)....
MySQL Reserved Keyword List
time is a restricted word, does this help:
INSERT INTO table_a (`fb_uid`, `x`, `y`, `time`) VALUES (12345,'blah','test','2012-12-13 11:30:00')
Escaping everything to be sure.

SQL MAX in conjunction with WHERE clause

I need to find the largest value from one particular column in a mysql table where the value of another column is equivalent to something. But, with the query that I'm using, I keep on getting a message that displays and SQL error. My query is aS follows:
SELECT MAX(message_id) AS top_message HAVING(child_id) = '".$message_id[$i]."'
Any suggestions?
You are also missing a table name:
SELECT MAX(message_id) AS top_message FROM tablename WHERE child_id = '".$message_id[$i]."'
You should use WHERE instead of HAVING Clause:
SELECT MAX(message_id) AS top_message
FROM tablename
WHERE child_id = '".$message_id[$i]."'
Use only HAVING clause when you have an aggregated conditon.
You need a from clause and a where clause. The having clause is used for group filters. You don't have a group by clause, so there is no reason to write a having clause. If the table where you want to select from is called 'MyTable', then your query is as follows:
SELECT MAX(message_id) AS top_message
FROM MyTable
WHERE child_id = '".$message_id[$i]."'
Note, that the paranthesis around child_id is not needed. Please read SQL and MySQL tutorials for more information, your life will be much easier.

mistake in mysql statement

I cant find where my mistake is.
$result= mysql_query(" INSERT INTO inbox ( messages, from, to, date, control_panel_id, title )
VALUES( '".$message."' , '".$this->sender."' , '".$this->recipient."', NOW() , '".$this->control_panel_id."' , '".$title."' )
") or die(mysql_error());
I get:
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, to, date, control_panel_id, title ) VALUES( ' ' at line 1
What am I doing which is wrong?
You need to quote reserved words like from when using them as names or aliases. You can do this by surrounding them with backticks, for example:
SELECT messages, `from`, ...
If in doubt, you can safely quote all column names.
INSERT INTO `inbox`
(`messages`, `from`, `to`, `date`, `control_panel_id`, `title`)
VALUES
-- etc...
Also, you may wish to consider avoiding names that are reserved worsd in future to avoid problems like this.
from is a reserved word in SQL. If that's a column name, you always have to enclose it in backticks. (Or double quotes for ANSI mode).
You could also write your mysql_query string less cumbersome by actually utilizing the double quotes:
$result = mysql_query("
INSERT INTO inbox
( messages, `from`, `to`, `date`,
control_panel_id, title )
VALUES
( '$message', '$this->sender', '$this->recipient',
NOW() , '$this->control_panel_id', '$title' )
")
or die(mysql_error());
(And contemporary PHP database interfaces would be even less effort, bla bla..)
Mysql not suppot some keyword as it used in mysql syntax so i think you have change
keywords from, to and date to other name...

Categories