Error with not exists code - mariaDB - php

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')

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;

Use of CONCAT in FROM clause

I have a non-working query as the sub-query FROM clause doesn't understand CONCAT():
SELECT *
FROM `events` e
WHERE EXISTS (SELECT *
FROM CONCAT('prefix_', e.`event_id`) registrations
WHERE registrations.`attendee` = 123456
)
Is there any way to make this work in a single-statement?
The error message I receive is:
#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 '('prefix_', e.event_id) registrations
You are trying to dynamically come up with a table name? I don't think this is possible...because doesn't the FROM clause table name need to be resolved prior to the SQL being able to be evaluated and executed?
SELECT * FROM (SELECT *,
CONCAT('prefix_', `event_id`) as prefix FROM `events`
WHERE events.attendee = 123456
);

MySQL NOT EXITS condition not working

Hello I am trying to execute a mysql query but getting error which I can't understand. The query I am using is
INSERT INTO summary (oid,tab,cost) VALUES('1','7','40') WHERE NOT EXISTS (SELECT * FROM summary WHERE cusid ='1')
I am using this tutorial as my reference, http://www.techonthenet.com/mysql/exists.php
This is the structure of the summary table
This is 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 new 'WHERE NOT EXISTS (SELECT * FROM summary WHERE cusid = 1)' at line 1
It is not exits. Change it to exists.
INSERT INTO summary (oid,tab,cost) VALUES('1','7','40') WHERE NOT EXISTS (SELECT * FROM summary WHERE cusid ='1')
^
Also you can't use NOT EXISTS with your above query, you can with a subquery.

execute multiple query with temp table in php

I have the following query, to copy a row within a table and alter a few columns.
CREATE TEMPORARY TABLE temp_table AS
SELECT *
FROM table1
WHERE offertecode = '1c12a23453453458e492230df420972';
UPDATE temp_table
SET offertecode = '82a24c7da2342423424351804ab043',
id = NULL,
reference = '[COPY] subject';
INSERT INTO table1
SELECT *
FROM temp_table;
DROP TEMPORARY TABLE temp_table;
This works perfectly fine in phpmyadmin, but I cant get it to work from within PHP, I get an error:
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 'UPDATE temp_table SET offertecode = '82a24c7da2342423424351804ab043',
id = ' at line 5
Can anyone help me on how to execute this query in PHP?
PHP code:
$mysqli->query("CREATE TEMPORARY TABLE temp_table AS
SELECT *
FROM table1
WHERE offertecode = '1c12a23453453458e492230df420972';
UPDATE temp_table
SET offertecode = '82a24c7da2342423424351804ab043',
id = NULL,
reference = '[COPY] subject';
INSERT INTO table1
SELECT *
FROM temp_table;
DROP TEMPORARY TABLE temp_table;");
Thanks!
This doesn't address the mysqli problem (which is having four statements in a single query). You should run those separately. But, you don't need four statements. Just do:
insert into table1(offertecode, id, reference, <rest of columns>)
select '82a24c7da2342423424351804ab043' as offertecode, NULL as id, '[COPY] subject' as reference,
<rest of columns>
from table1
where offertecode = '1c12a23453453458e492230df420972';
An insert . . . select statement can refer to the same table in both parts. Even in MySQL.
You need to use $mysqli->multi_query. The basic answer is you cannot run multiple queries using $mysqli->query. The syntax error is due to a second query trying to to be run under $mysqli->query which only allows one main query. That query can contain sub or nested queries, but only one main query. To run multiple queries you have to use $mysqli->multi_query

MySQL Distinct Query Error

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.

Categories