Mysql Insert Where Column is Like Wildcard Query - mysql

I'm attempting to inset values into every row where one of the column values contains a specific string.
INSERT INTO `cases_index`(`related`) VALUES (`Alabama`) WHERE `court` LIKE '%Alabama%'
So int eh above example I have a row with three columns:
court | federal | related
I want to insert the word 'Alabama' into the 'related' column for any row where the court column contains the word 'Alabama' (i.e. 'District Court of Alabama').
The above line spits out the following syntax error:
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 cases_index(court) LIKE '%Alabama%'' at line 1
Any ideas what I am doing wrong?

Apart from the syntax error in your query, if you want to update existing rows you should use an UPDATE query:
UPDATE cases_index
SET related='Alabama'
WHERE court LIKE '%Alabama%'
Just for completeness, INSERT queries add new rows to the table, so a WHERE clause in an INSERT is not allowed.

While the UPDATE answers are what you are looking for, INSERT does allow WHERE clause.
Refer to MySQL documentation
INSERT INTO tbl_temp2 (fld_id)
SELECT tbl_temp1.fld_order_id
FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;
But the answer to this is what trgdor wrote:
UPDATE cases_index
SET related='Alabama'
WHERE court LIKE '%Alabama%'

Related

SQL: Unrecongnized statement near conditional (IF)

I've never used IF's before in SQL. I need to update a row where institution is a specific number if it exists and insert it if it doesn't. In order to avoid using first a select and then a insert or update I wanted to try my hand at an IF statement. I figured from what I've read in the documentation that it should go something like this:
IF (NOT EXISTS(SELECT evaluations FROM tEvaluations WHERE institution = 0))
BEGIN
INSERT INTO tEvaluations (institution,evaluations) VALUES (0,0)
END
ELSE
BEGIN
UPDATE tEvaluations SET evaluations = 10 WHERE institution = 0
END
However I get this error:
#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 'BEGIN
INSERT INTO tEvaluations (institution,evaluations) VALUES (0,0)
END' at line 2
I'm trying to run this query in phpmyadmin to test out how the query should be.
You can't have if..else block in normal SQL statement unless it's inside a procedural block. To me looks like you are looking for INSERT ON DUPLICATE KEY UPDATE like
INSERT INTO tEvaluations (institution,evaluations) VALUES (0,0)
ON DUPLICATE KEY UPDATE evaluations = 10;
Per documentation, either of your column should have a UNIQUE constraint defined against it. Quoting from documentation
If you specify an ON DUPLICATE KEY UPDATE clause and a row to be
inserted would cause a duplicate value in a UNIQUE index or PRIMARY
KEY, an UPDATE of the old row occurs. For example, if column a is
declared as UNIQUE and contains the value 1

opposite of insert into - removing duplicates

I am very new to Sql, so bear with me. I have run a query the following query:
INSERT INTO adhoc_dt.`table` (id, name) VALUES(53098974, 'John');
however, by accident I run it twice. I would like to remove the duplicate. How can that be done?
I tried
INSERT INTO adhoc_dt.`table` (id, name) VALUES(53098974, 'John');
but get an error:
SQL ERROR [1064][42000]: 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
If your table name include special char of reserved word then you should enclose the table name with backticks:
`table`
But looking to your question, the table name seems:
`adhoc_dt.`table`
In this case, the correct syntax for delete is:
DELETE FROM adhoc_dt.`table`
WHERE id = 53098974 AND name = 'JOHN'
but in this way you delete all the rows with:
id = 53098974 AND name = 'JOHN'
You have to follow steps
truncate table
apply unique constraint on ID
run insert script

select inside insert with more data to be inserted

I'm facing a problem with select inside insert statements.
I've take a look at the questions similar to this but still the query is not working.
first, I'm working with MySQL version 5.6.24 with engine InnoDB, and I'm trying to insert this row:
INSERT INTO form (SELECT course_name FROM course WHERE course_id ='1' ), '123456','5','3','6','1','3','6','1','2','5','6','1','4','1','2','3','good','not bad','bad')
I want the first column to be retrieved (which is only one value), but not the other.
I've tried many syntax formats, with VALUES, with semicolon, with more parentheses, etc... but non work. Here is the error:
#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 ' '123456','5','3','6','1','3','6','1','2','5','6','1','4','1','2','3','good','no' at line 1
Thanks.
Use an INSERT-SELECT:
INSERT INTO form
SELECT course_name, '123456','5','3','6','1','3','6','1','2','5','6','1','4','1','2','3','good','not bad','bad'
FROM course
WHERE course_id = '1'

Multiple Queries in Triggers

I'm fairly new to using triggers and have a tiny question.
I have a trigger finds a match between a newly inserted enquiry and a customer table.
INSERT INTO customersmatched (customerID,enquiryID) SELECT id, NEW.id FROM customer AS c WHERE c.customerName=NEW.companyName HAVING COUNT(id)=1;
I then need to update the newly inserted enquiry so it has a status which shows it's matched (but only if it has matched). So I tried adding this line after the insert.
UPDATE enquiry SET status="Live-Enquiry" WHERE id IN ( SELECT enquiryID FROM customersmatched WHERE enquiryID = NEW.id);
Except I get this error:
MySQL said: #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 'UPDATE enquiry SET status="Live-Enquiry" WHERE id
IN ( SELECT enquiryID FROM cus' at line 5
How do I allow multiple queries within a trigger. I've tried doing something like in this link: Multiple insert/update statements inside trigger?
But doesn't work either. I'm using phpmyadmin btw. Can anyone help? :D
If you have ansi quotes enabled then you can't use double quotes as a string literal, and need to use single quotes instead. see: http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_ansi_quotes Otherwise, I don't see any syntax errors that jump out at me.
Try changing SET status="Live-Enquiry" to SET status='Live-Enquiry'
EDIT:
What is the purpose of the first query? I'm not sure you need the HAVING in that query. If want a distinct list of matches, just use DISTINCT
INSERT INTO customersmatched (customerID,enquiryID)
SELECT DISTINCT id, NEW.id
FROM customer AS c
WHERE c.customerName=NEW.companyName;
The second query, if I understand it correctly, can be simplified to this:
UPDATE enquiry
SET status='Live-Enquiry'
WHERE id = NEW.id;

INSERT values from one table to another using SQL Query

I am trying to run this SQL Query:
INSERT INTO controldata (field,value)
VALUES (customer_billing_product_type,
SELECT name from customer_billing_product_types)
to insert all rows in the customer_billing_product_types table into the controldata table with the field always being customer_billing_product_type but i get an SQL Error saying:
#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
'SELECT name from customer_billing_product_types)' at line 1
The INSERT as you wrote it expects to insert one record, but then in values an entire set of records is returned. Try to change your query to
INSERT INTO controldata(field, value)
SELECT customer_billing_product_type, t.name FROM customer_billing_product_types t;
This selects all t.name values from customer_billing_product_types, adds customer_billing_product_type as column and inserts all the results into controldata.
You are using wrong syntax try below:
INSERT into controldata (field1) SELECT name from customer_billing_product_types;
If you want to keep a field as constant then you can use as per below:
INSERT into controldata (field,value) SELECT 'customer_billing_product_types',name from customer_billing_product_types;