mysql function in query with dynamic sql involved - mysql

How to use a stored function instead of an explicitly written table name.
My query:
select "col_name" from get_table("col_name") where id = 2;
get_table(col_name) is my stored function which returns the table name after having processed column name.
I get error message
Error Code: 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 '("col_name") where id = 2' at line 1
I also tried to do like this:
DECLARE tableName varchar(30);
set tableName = get_table("col_name");
...
select "col_name" from tableName where id = 2;
This time I get error:
no such a table - tableName

Related

Check if record exists in table before inserting new data

SQL Noob here.
Im using PHP PDO with named placeholders to insert data to my DB but I want to check if the data exists before I insert it.
Here is my statement:
IF NOT EXISTS (SELECT * FROM dc_line_items_transactions WHERE order_id = '3784732' AND product_id = '435062') INSERT INTO dc_line_items_transactions (order_id,refund_id,user_id,transaction_date,product_id,line_item_id,line_subtotal,line_vat,line_total,dc_timestamp) VALUES (:order_id,:refund_id,:user_id,:transaction_date,:product_id,:line_item_id,:line_subtotal,:line_vat,:line_total,:dc_timestamp)
My PDO code works fine as Its being used by other classes to insert varying different types of data.
My error message is as follows:
SQLSTATE[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 'IF NOT EXISTS (SELECT * FROM dc_line_items_transactions WHERE order_id= '3784732' at line 1

I want to migrate specific data of one table to another specific column which is in another database in MySql

I just use the following query but it shows error. Any suggestions?
INSERT INTO test1.`tbl_news`.`file`
SELECT * FROM test2.`tbl_download_media`.`media`
WHERE TYPE = 'event'
Here I want to copy data from table tbl_download_media which have column media and type event of DB test2 to DB 'test1' having table tbl_news with column file
Here is the error:
Error Code: 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 '.file select * from
nic_data.tbl_download_media.media where type = 'event' at line 1
Your insert statement has some minor syntaxis errors. This is the corrected version:
INSERT INTO test1.tbl_news(`file`)
SELECT media FROM test2.tbl_download_media
WHERE TYPE = 'event'
Now, if you want to update based on same id for event type:
UPDATE test1.tbl_news AS b
INNER JOIN test2.tbl_download_media AS g
ON (b.id = g.id)
SET b.file = g.media
WHERE TYPE = 'event'

Update longtext field in mysql

Im trying to update a longtext type field called 'comment' using a simple sql query in mysql client like this :
Update mytable set comment='Test' where id = 1;
But i'm getting this 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 'comment='Test' where id = 1' at line 1
Am i missing something ?, thanks in advance.
comment is a reserved word, if you want to have a table/field with that name, you have to quote it (or use the table.fieldname syntax, in case of a field). default in mysql is the backtick for that, so:
update mytable set `comment`='Test' where id = 1;
Found it, it gets solved with this:
update mytable as a set a.comment='Test' where id = 1;

How to do pattern matching to a variable in mysql

So im creating a procedure that accepts an argument namee and returns all users whose name is a substring of namee
create procedure search_res(IN namee varchar(50))
begin
select * from usr where name like %namee%;
end
But im getting the following 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 '%namee%; end' at line 3.
What is the correct syntax?
Use CONCAT function
select * from usr where name like CONCAT('%',namee,'%');

MySQL: Resetting AUTO_INCREMENT

I am trying to reset the auto increment value in one of my tables based on the number of rows currently in it. Here is the code I have so far.
SET #numrows = 0;
SELECT COUNT(*) total, #numrows := COUNT(*) + 1 numrows FROM maj_user ;
ALTER TABLE `maj_user` AUTO_INCREMENT = #numrows ;
This works great if I execute it in MySQL Workbench. However, I need to save this as an SQL file and execute it as part of a database import script. If I do this, I get this:
ERROR 1064 (42000) at line 39: 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 '#numrows' at line 1
Line 39 is the ALTER TABLE statement. Any ideas?
Can you change your syntax to skip actually setting #numrows? I'm not sure what the problem is but a workaround seems to be something like:
ALTER TABLE `maj_user` AUTO_INCREMENT = (SELECT COUNT(*) + 1 from maj_user);