i need Update Query from Mssql to Mysql in LINKED Server - mysql

Actually i need to connect Mssql to Mysql , Fortunately i did with MSSQL 2008 R2 linked server i made connection for Mysql
Now i want to write some queries for update at a time in both databases
when i am trying this query
update products set Stock=A.Stock from
(Select * FROM OPENQUERY(MYSQL,'Select * From products where Id=8')) A
inner join products B on b.Id=a.Id
the rows of MSSQL is updated from MYSQL
i need to update MSSQL to MYSQL also
please help me out ,i am working since last 4 days

MYSQL TO MSSQL UPDATION:
update products set Stock=A.Stock from
(Select * FROM OPENQUERY(MYSQL,'Select * From products')) A
inner join products B on b.Id=a.Id
UPDATE employee
SET LastName = ( Select FirstName from employee where FirstName = (SELECT * FROM
OPENQUERY(MYSQL, 'Select FirstName from employee where IndividualId=3')))
MSSQL TO MYSQL UPDATION:
UPDATE OPENQUERY (database, 'SELECT Stock FROM wings.products WHERE id =1')
SET Stock=999;

Related

Syntax Error MySQL Update

I'm running a pass-through from access to MYSQL and trying to run an update query but keep getting a syntax error. I am brand new to MYSQL. It looks correct to me so I have no clue what I'm doing wrong. The query works in Access.
Access Query:
UPDATE [ProductInformation-ODBC]
INNER JOIN TempPrice
ON [ProductInformation-ODBC].FirstOfSku =TempPrice.FirstOfSku
SET [ProductInformation-ODBC].Price = [TempPrice]![Price];
MYSQL:
UPDATE ProductInformation-ODBC
INNER JOIN TempPrice ON ProductInformation-ODBC.FirstOfSku =
TempPrice.FirstOfSku
SET ProductInformation-ODBC.Price = TempPrice.Price
UPDATE `ProductInformation-ODBC` P
INNER JOIN TempPrice ON P.FirstOfSku =
TempPrice.FirstOfSku
SET P.Price = TempPrice.Price
Don't use - in the table name and choose a proper name for your tables
In the complex queries use an alias name for your tables like P on the code above

MySQL IF EXISTS not working

Trying to use IF EXISTS to automatically choose which table to select a record from.
I just get the following error message with the code below.
Where am I going wrong?
IF EXISTS (SELECT 1 FROM Users WHERE Reference='USR00000007')
SELECT * FROM Users WHERE Reference='USR00000007'
ELSE
SELECT * FROM Staff WHERE Reference='USR00000007';
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 'IF EXISTS (SELECT 1 FROM Users WHERE Reference='USR00000007') SELECT * FROM' at line 1
The problem is in MySQL. The IF -- as control flow -- only works in programming blocks such as stored procedures, stored functions, and triggers.
Assuming the columns are the same in both tables, you can do what you want as a single query:
SELECT u.*
FROM Users u
WHERE u.Reference = 'USR00000007'
UNION ALL
SELECT s
FROM Staff s
WHERE s.Reference = 'USR00000007' AND
NOT EXISTS (SELECT 1 FROM Users u2 WHERE u2.Reference = 'USR00000007')
if you want union of two query, both mast have same number of fields (if you need, you can add null fields to query) and fields should have similar type, so try this:
SELECT Users.id as user_id, null as staff_id, some_other_number_field, some_other_char_field
FROM Users
WHERE Reference='USR00000007'
SELECT null , Staff.id, some_other_number_field, some_other_char_field
FROM Staff
WHERE Reference='USR00000007'
and not EXISTS (SELECT 1 FROM Users WHERE Reference='USR00000007');
maybe you need query like this, to always get only users for Reference:
SELECT *
FROM Users
WHERE (Reference='USR00000007'
or exists (
SELECT 1
FROM Staff
WHERE Reference='USR00000007'
and Staff.user_id = Users.id));
but you must have join condition similar to "and Staff.user_id = Users.id"

Delete duplicates for multiple columns in JOIN on same table

I am trying to make a delete from joined same table like this:
DELETE FROM `sp10_seo_url` AS sp1 JOIN
(
SELECT seo_url_pk, COUNT(*) AS maxc
FROM `sp10_seo_url`
GROUP BY seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
HAVING maxc > 1
) AS sp2
ON sp1.seo_url_pk = sp2.seo_url_pk
However I am getting a mysql 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 'AS sp1 JOIN ( SELECT seo_url_pk, COUNT(*) AS maxc FROM `sp10_s' at line 1
And I am not sure at all where the error is. The inner query runs just fine and returns the expected set of results. The "ON" keys are properly named (same since we are talking about the same table).
I guess the idea of the query is pretty clear (clean the table of different rows have the same set of values for the three "group by" columns. Is there another way to do this?
Thanks!
you can "cheat" mysql with a double indirection (as explained here Deleting a row based on the max value):
delete from `sp10_seo_url`
where seo_url_pk in (
select seo_url_pk from (
SELECT seo_url_pk
FROM `sp10_seo_url` sp1,
(
SELECT seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
FROM `sp10_seo_url`
GROUP BY seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
HAVING count(*) > 1
) sp2
where sp1.seo_url_entity_type = sp2.seo_url_entity_type
and sp1.seo_url_entity_id = sp2.seo_url_entity_id
and sp1.seo_url_language_fk = sp2.seo_url_language_fk
) t
);
http://sqlfiddle.com/#!2/899ff5/1

Issue in MySQL stored procedure from 5.0 to 5.1

When I upgraded from MySQL 5.0 to 5.1, the following SQL statement in an SP stopped working with error code 1064 - invalid column name VARCLNO in WHERE clause:
SELECT *, CLNO as VARCLNO, concat(CLCompany, CLSurname, CLFirstName, CLNO) as CLSort FROM Customer WHERE
CLEditDate = (select max(CLEditDate) from Customer WHERE VARCLNO = CLNO )
ORDER BY CLSort;
The CUSTOMER table columns all begin CL.
Any help much appreciated.
Try this query using table alias:
SELECT *,
CLNO as VARCLNO,
concat(CLCompany, CLSurname, CLFirstName, CLNO) as CLSort
FROM Customer AS T1
WHERE
CLEditDate = (select max(CLEditDate) from Customer WHERE T1.CLNO = CLNO )
ORDER BY CLSort;

How do you run a MySQL query from a rake task?

I'm working on removing duplicates from a legacy database for a client of mine, and I've found a MySQL query to do just that. I want to create a Rake task to run the query for the production server. How would I do that?
MySQL query:
select * from community_event_users;
create table dups as
select distinct username, count(*)
from community_event_users group by username
having count(*) > 1;
delete community_event_users from community_event_users inner join dups
on community_event_users.username = dups.username;
insert into community_event_users select username from dups;
If you are on Rails and using ActiveRecord you can simply use:
ActiveRecord::Base.execute(my_sql)
ActiveRecord::Base.connection.execute(my_sql)
Where my_sql is your SQL string.
For Rails 5 and above you better use:
ApplicationRecord.connection.execute <<~END_OF_SQL
YOUR QUERY HERE
END_OF_SQL