Thursday, February 16, 2012

cross platform derived table query

It's like this, I have a table of searchstrings that I want to list based on unique searchstrings and unique users(because some users search for the exact same thing many times)

table tbl_savesearch
id, searchStr, remote_addr, was_answered

This is the query that works on MS SQL server, but I can't get to work in MySQL:

SELECT ss, COUNT(DISTINCT ra) AS theCount
FROM (SELECT DISTINCT searchStr AS ss, remote_addr AS ra FROM tbl_savesearch) tb
GROUP BY ss
ORDER BY theCount DESC

I checked the mysql documentation and it seems mysql needs to put in an "as" before "tb", but it still won't work.

Is there a standard for this question, some way that I can make it work on both platforms?

Edit: is this thing really called derived table or is it just a subquery? English is not my native language.A subquery in the FROM clause is usually called an "inline view" (or at least, it is in Oracle circles). Whether mySQL supports inline views at all, I don't know.

No comments:

Post a Comment