Unable to delete rows based on key in another table - mysql

This is working:
select * FROM Work_Request A where EXISTS ( select 1 from Customer B where (B.lsource&128)!=0 and B.license_id=A.license_id) ;
As per Delete all rows in a table based on another table this delete should also work but instead giving error:
delete FROM Work_Request A where EXISTS ( select 1 from Customer B where (B.lsource&128)!=0 and B.license_id=A.license_id) ;
Error:
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 'A where EXISTS ..
...

your delete statement should be like this
delete A.* FROM Work_Request A
where EXISTS ( select 1 from Customer B where (B.lsource&128)!=0 and B.license_id=A.license_id) ;
you are missing name of table thats gonna delete ...have fun :-)

Related

Check before adding the data into table in mysql database

I am trying to insert data in table emp_master where token_no and vehicle_no is available, I am using mentioned below query to add and check the record
insert into emp_master (token_no, vehicle_no) values (1234,12345) where not exists (select * from emp_master where vehicle_no = '12345');
but whenever I am trying this then error comes in as
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 'where not exists (select * from emp_master where vehicle_no = '12345')' at line 1
The suggestion will be helpful
If you need to have this check in your insert statement, you can structure it like this:
SQL Fiddle: http://sqlfiddle.com/#!9/d3dd77/1
insert into emp_master (token_no, vehicle_no)
select 1234, 12345
from dual
where not exists (select * from emp_master where vehicle_no = '12345');
Another way to do this on any DBMS:
DB Fiddle Demo
insert into emp_master (token_no, vehicle_no)
select token_no, vehicle_no
from (
select 1234 token_no, 12345 vehicle_no
) rec
where not exists (select * from emp_master where vehicle_no = rec.vehicle_no);
Put a UNIQUE constraint on the vehicle_no column, and then insert the data with no WHERE clause. Instead, handle the exception if it fails.

Mysql LIKE %...% together with table

I have 2 tables with phone numbers.
In Table A I have to delete all phone numbers thats not in Table B.
In Table B the phonenumbers are with conterycode, but in Table A they are without conterycode.
I therefor try to use a command similar to LIKE %Phonenumber%
I have tried this code, but that is giving me a syntax error
DELETE FROM temp_table
WHERE NOT EXISTS (
SELECT *
FROM TableB
WHERE WorkTelephoneNumber LIKE temp_table.%PhoneNumber%
);
#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 '%PhoneNumber%
)' at line 5
You can try below -
DELETE FROM temp_table
WHERE NOT EXISTS (
SELECT *
FROM TableB
WHERE WorkTelephoneNumber LIKE concat('%',temp_table.PhoneNumber,'%')
);
A subquery will be very slow. you can use join instead
Delete t1 from temp_table t1
join TableB on tableB.WorkTelephoneNumber like concat('%',t1.PhoneNumber,'%')

How to create trigger for updating in mysql

How to Update table1 if the table1 primary key existing in table2. I am using following code but it gives mysql syntax error.
CREATE TRIGGER upd_selectoin
BEFORE UPDATE ON customer
FOR EACH ROW
BEGIN
IF NEW.customer_sk IN(SELECT quotation_cname FROM quotation) THEN
UPDATE customer s JOIN quotation m
ON m.quotation_cname = s.customer_sk
SET s.grade = 2
WHERE s.customer_sk = NEW.customer_sk;
END IF;
I got the following 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 '' at line 9
I want to update the customer table grade column if the customer_sk existing in quotation table.Please help me

Can aliases be used in a SQL delete query?

I have the following SQL query:
DELETE FROM table_b b WHERE NOT EXISTS (SELECT * FROM table_a a WHERE a.some_id = b.some_id)
and am getting the following error:
[Err] 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 'b WHERE NOT EXISTS(SELECT * FROM table_a a WHERE a.some_id =
b.some_id)' at line 1
this seems to suggest that aliases cannot be used with SQL delete statements (?)
Yes you can use alias in DELETE query. Just you have use that alias after DELETE keyword than it will work. It specifies that from which table you have delete the records.
Try this:
DELETE b
FROM table_b b
WHERE NOT EXISTS (SELECT * FROM table_a a WHERE a.some_id = b.some_id)
If I'm understanding your query correctly and your wanting to delete all records that are not in table a from table b. A cleaner way to write it might be
DELETE FROM table_b WHERE id NOT IN (SELECT id FROM table_a)

mysql insert - select syntax error

This is my query -
$q = "CREATE TEMPORARY TABLE tmp SELECT * from category c WHERE category_id IN (SELECT category_id FROM category_to_store WHERE store_id = '".(int)$from_store_id."');
ALTER TABLE tmp drop category_id;
INSERT INTO category SELECT 0,tmp.* FROM tmp;
SET #last_id_in_category := LAST_INSERT_ID();
select #last_id_in_category;
DROP TABLE tmp;"
mysql_query($q);
I am getting this error on execution
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 'ALTER TABLE tmp drop category_id; INSERT INTO category SELECT 0'
at line 2 Error No: 1064
But when I run the query in database directly then I am not getting any error.
Please help me !
From the php docu on `mysql_query'
mysql_query() sends a unique query (multiple queries are not supported) to the currently active database ...
So basically split your queries to multiple calls of `mysql_query' each with a single query and you should be fine.