-1

I have a MSSQL query and would like to convert it to MySQL version. But I have an error in my console:

#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 'DECLARE @selectedTags TABLE (ID int) DECLARE @tagCount int INSER' at line 1

My code below:

DECLARE @selectedTags TABLE (ID int)
DECLARE @tagCount int

INSERT INTO @selectedTags VALUES (1)
INSERT INTO @selectedTags VALUES (3)
INSERT INTO @selectedTags VALUES (5)

SELECT @tagCount = COUNT(*) FROM @selectedTags

SELECT
    P.ID
FROM Product P
JOIN ProductTag PT
    ON PT.ProductID = P.ID
JOIN @selectedTags T
    ON T.ID = PT.TagID
GROUP BY
    P.ID,
    P.Name
HAVING COUNT(PT.TagID) = @tagCount
XTRUST.ORG
  • 3,280
  • 4
  • 34
  • 60
  • http://stackoverflow.com/questions/1524858/create-table-variable-in-mysql – Donal Aug 18 '14 at 15:45
  • put ; at the end of lines for the begining – catalinetu Aug 18 '14 at 15:50
  • 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 'DECLARE @selectedTags TABLE (ID int)' at line 1 – XTRUST.ORG Aug 18 '14 at 15:52

1 Answers1

0

There is no TABLE type in Mysql, you have to CREATE TEMPORARY TABLE and INSERT to it.

DECLARE is allowed in stored routines only.
to set @tagCount simply use

SELECT COUNT(*) INTO @tagCount FROM temp_table;
Naktibalda
  • 13,705
  • 5
  • 35
  • 51