I am converting a Query from SQL Server to MySQL but I have a problem converting the sentence contains table , because it uses rank .
I´ve looked for a similar property but I didn´t find anything , here is my Query at SqlServer
SELECT KEY_TBL.RANK
FROM CATS
INNER JOIN containsTABLE (CATS,(COLOR,CITY),'ORANGE',1000) AS KEY_TBL ON RUP.ID = KEY_TBL.[KEY]
ORDER BY RANK
Related
I keep getting a syntax error "WITH is not valid input in this position" for WITH RECURSIVE. I've seen a similar problem here, where people back in 2018 said MySQL will support CTE's in MySQL version 8. Some other sources also refer that CTE should work for MySQL since version 8.0. My current version is 8.0.27, however, I still can't use WITH RECURSIVE in MySQL Workbench.
The query I need to retrieve the family tree is:
WITH RECURSIVE family_path (individual_id, first_name, path) AS
(
SELECT individual_id, first_name, first_name as path
FROM individual
WHERE parent IS NULL
UNION ALL
SELECT c.individual_id, c.first_name, CONCAT(fp.path, ' > ', c.first_name)
FROM family_path AS fp JOIN individual AS c
ON fp.individual_id = c.parent
)
SELECT * FROM family_path
ORDER BY path;
Is there any way for me to solve this problem?
I have a select statement running in a jsp against SQL Server (previously using MySql without issues).
the TOP 1 was added because otherwise SQL Server moans about order by clauses (but only when displaying a result in a jsp, not when running the query inside SQL Server Management Studio).
This query runs fine in SQL Server Management Studio
SELECT TOP 1
alerts.id,
alerts.ts,
asset_firstname,
asset_lastname,
assetid,
alerttype.name,
node.zonename,
node.ipaddress,
node.zonegroupid
from
alerts, asset, alerttype, node, alertrules
where
ack=0 and
alerts.nodeid = node.id and
alerts.alerttypeid = alerttype.id and
alertrules.alerttypeid = alerts.alerttypeid and
alerts.assetid = asset.id and
alerts.alerttypeid = 1 and
asset.id=1157 and
alertrules.userid = 1
order by alerts.ts desc
but, when run in the jsp it returns "Column alerts.ts is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause".
I don't want alerts.ts aggregated or grouped by, hence the 'correct' select statement.
If I remove TOP 1 or alerts.ts desc the query returns the wrong row (earliest rather than latest record)
Converting what should be straightforward basic SQL commands so they run properly with SQL Server is proving a nightmare.
Any thoughts appreciated.
Regards
Ralph
(I wrote this as an answer because as a comment would be a mess)
You are using old style joins, and have redundant checks. Maybe this could make a difference (not sure, as it seems to be a problem related to JSP):
SELECT TOP(1)
alerts.id, alerts.ts,
asset_firstname,
asset_lastname,
assetid,
alerttype.name,
node.zonename,
node.ipaddress,
node.zonegroupid
from alerts
inner join asset on alerts.assetid = asset.id
inner join alerttype on alerts.alerttypeid = alerttype.id
inner join node on alerts.nodeid = node.id
inner join alertrules on alertrules.alerttypeid = alerts.alerttypeid
where ack=0 and
alerts.alerttypeid = 1 and
asset.id=1157 and
alertrules.userid = 1
order by alerts.ts desc;
i'm trying to migrate a mysql view from mysql to sql server 2008, but there is a funcion called "group_concat" and i cant emulate it in sql server.
Here is the code
select
permiso.ID AS ID,replace(
group_concat(cuerpo_legal.descripcion, _utf8' ',
documento_legal.NUM_CUERPO_LEGAL, _utf8' / ',
documento_legal.ANNO_CUERPO_LEGAL separator ',')
,_utf8',', _utf8' ; ') AS cuerpo_legal
from
(((permiso
join permiso_doc_legal ON ((permiso_doc_legal.ID_PERMISO = permiso.ID)))
join documento_legal ON ((documento_legal.ID = permiso_doc_legal.ID_DOCUMENTO_LEGAL)))
join cuerpo_legal ON ((cuerpo_legal.id_cuerpo_legal = documento_legal.ID_CUERPO_LEGAL)))
group by permiso.ID
I've tried diferent ways and searched here in stackoverflow but none one works
Thanks!
I am trying to convert an MS Access query to MySQL and the problem is converting MS Access top to MySQL limit to get the same result. When I change query to limit I get the error that this version of MySQL does not support limit in subquery.
This is the MS Access query:
SELECT a.FK_CONTRIBUTOR_ID
FROM tPUBLISHERS
INNER JOIN (tCONTRIBUTORS AS b
INNER JOIN tCLIPS AS a ON b.CONTRIBUTOR_ID = a.FK_CONTRIBUTOR_ID)
ON tPUBLISHERS.PUBLISHER_ID = b.FK_PUBLISHER_ID
WHERE ((a.CLIP_ID) In
(select top 5 CLIP_ID
from tCLIPS
where FK_CONTRIBUTOR_ID = a.FK_CONTRIBUTOR_ID
AND SUSPEND = a.SUSPEND))
AND ((a.FK_CONTRIBUTOR_ID) In (1922,2034,2099))
Previously answered at:
MySQL Subquery LIMIT
basically change the subquery to a Join
Google for more with "mysql limit on subquery"
Following query runs well in MySQL 5.x
SELECT
m_area.id, m_area.cn_areaName, m_area.de_areaName,
m_area.en_areaName,m_area.jp_areaName,t_shop.count
FROM
m_area left join
(
select t_shop.areaID, count(areaID) AS count
from t_shop
group by t_shop.areaID
) t_shop
on m_area.id = t_shop.areaID
However, when I have to run it in a 4.0.23 MySQL DB with same DB structure and data it just return following message:
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 t_shop.areaID, count(areaID) AS count
from t_s
I tried many times but still failed. Is left join to subquery not allowed in MySQL 4.x ? Then that mean I have to do it with a temp table?
Thanks in advance!
Subqueries were quite not well supported with MySQL 4.0 : it became possible to use them (at least, in some real, useful way) with MySQL 4.1 -- and MySQL 4.0 is really old, now...
See for instance this page of the MySQL manual : 12.2.8. Subquery Syntax (quoting, emphasis mine) :
Starting with MySQL 4.1, all subquery forms and operations that the
SQL standard requires are supported,
as well as a few features that are
MySQL-specific.
With MySQL versions prior to 4.1, it
was necessary to work around or
avoid the use of subqueries. In
many cases, subqueries can
successfully be rewritten using joins
and other methods. See Section
12.2.8.11, “Rewriting Subqueries as Joins for Earlier MySQL Versions”.
take out ", count(areaID) AS count"
The multiple columns in the subquery is messing up the join.
A temp table should work fine ....
Have fun!
Only thing I could think of is adding the tablename to your areaID in the subquery or renaming the reserved word count to cnt.
SELECT m_area.id
, m_area.cn_areaName
, m_area.de_areaName
, m_area.en_areaName
,m_area.jp_areaName
,t_shop.cnt
FROM m_area
left join (
select t_shop.areaID
, count(t_shop.areaID) AS cnt
from t_shop
group by t_shop.areaID
) t_shop on m_area.id = t_shop.areaID