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;
Related
Here is an error I don't understand:
mysql> UPDATE gp
-> SET gp.gpid = gp.new_gpid
-> FROM (
-> SELECT gpid, ROW_NUMBER() OVER (ORDER BY [gpid]) AS new_gpid
-> FROM gp
-> ) gp;
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 'FROM (
SELECT gpid, ROW_NUMBER() OVER (ORDER BY [gpid]) AS new_gpid
' at line 3
As far as I can tell nested SELECT in a FROM statement seems to be depreciated.
Am using mysql 8.0.21
Any help to make this query work would be greatly appreciated.
Thank you
EDIT 1:
What I am trying to achieve is to update the gpid column with row numbers instead of the actual AUTO_INCREMENT id, which contains gaps in between ids, the explanation in this post Change mysql auto increment id column value
The UPDATE... FROM ... syntax is not supported by MySql.
You can do it with a JOIN:
UPDATE gp
INNER JOIN (
SELECT gpid, ROW_NUMBER() OVER (ORDER BY gpid) AS new_gpid
FROM gp
) t ON t.gpid = gp.gpid
SET gp.gpid = t.new_gpid;
See a simplified demo.
Your nested query is selecting from 'gp' but is then aliased as 'gp' it can't select from itself.
what is the error in below mysql query?
SELECT distinct d.temp,d.pre FROM Data as d
WHERE NOT EXISTS
(
SELECT distinct d1.temp,d1.pre FROM Data as d1 where
d1.temp <=d.temp AND d1.pre <>d.pre
AND (d1.temp < d.temp or d1.pre<d.pre)
)
As per your SQL Fiddle your query executes fine.
MySQL database (community) version: 5.6.27, Windows 7 Pro x64
I've just created this View:
DELIMITER $$
ALTER ALGORITHM=UNDEFINED DEFINER=`admin`#`%` SQL SECURITY DEFINER VIEW `vw_qb_assembly_component_info` AS (
SELECT
`qb_assembly_components`.`assembly_item_id` AS `assemblyId`,
`ai`.`name` AS `assemblyName`,
`qb_assembly_components`.`component_quantity` AS `componentQuantity`,
`qb_assembly_components`.`component_item_id` AS `item_id`,
`ci`.`name` AS `name`,
`ci`.`item_number_type` AS `item_number_type`,
`ci`.`type` AS `type`
FROM ((`qb_assembly_components`
JOIN `qb_items` `ai`
ON ((`ai`.`item_id` = `qb_assembly_components`.`assembly_item_id`)))
JOIN `qb_items` `ci`
ON ((`ci`.`item_id` = `qb_assembly_components`.`component_item_id`))))$$
DELIMITER ;
I am attempting to query the view for rows with a certain qb_assembly_components.assembly_item_id value. I've tried several variations of defining the column in the WHERE clause but always receive error:
Unknown column 'xyz' in 'where clause'
The following are the versions I've tried:
WHERE `qb_assembly_components`.`assemblyId` = 'RR-0T056'
WHERE `qb_assembly_components`.`assembly_item_id` = 'RR-0T056'
WHERE `assemblyId` = 'RR-0T056'
I'm stumped. I've googled a bit and found a few results that seem to suggest using the alias is the way to go (my last example in the above 3 examples) but it's not working.
What am I doing wrong?
If you are select from VIEWvw_qb_assembly_component_info``
then the where clause should refer to the view name (and not to the orginal table name)
WHERE `vw_qb_assembly_component_info`.`assemblyId` = 'RR-0T056'
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
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;