开发者

MySQL - return one row from 2 rows in the same table, overwrite the contents of the first 'default' with the populated fields of the second 'override'

I am trying to make use of the mobile device lookup data in the WUFL database at http://wurfl.sourceforge.net/smart.php but I'm having problems getting my head around the MySQL code needed (I use Coldfusion for the server backend). To be honest its really doing my head in but I'm sure there is a straightforward approach to this.

The WUFL is supplied as XML (approx 15200 records to date), I have the method written that saves the data to a MySQL database already. Now I need to get the data back out in a useful way!

Basically it works like this: firstly run a select using the userAgent data from a CGI pull to match against a known mobile device (row 1) using LIKE; if found then use the resultant fallback field to look up the default data for the mobile device's 'family root' (row 2). The two rows need to be combined by overwriting the contents of (row 2) with the specific mobile device's 开发者_如何学Pythonfeatures of (row 1). Both rows contain NULL entries and not all the features are present in (row 1).

I just need the fully populated row of data returned if a match is found. I hope that makes sense, I would provide what I think the SQL should look like but I will probably confuse things even more.

Really appreciate any assistance!


This would be my shot at it in SQL Server. You would need to use IFNULL instead of ISNULL:

SELECT
    ISNULL(row1.Feature1, row2.Feature1) AS Feature 1
    , ISNULL(row1.Feature2, row2.Feature2) AS Feature 2
    , ISNULL(row1.Feature3, row2.Feature3) AS Feature 3
FROM
    featureTable row1
    LEFT OUTER JOIN featureTable row2 ON row1.fallback = row2.familyroot
WHERE row1.userAgent LIKE '%Some User Agent String%'

This should accomplish the same thing in MySQL:

SELECT
    IFNULL(row1.Feature1, row2.Feature1) AS Feature 1
    , IFNULL(row1.Feature2, row2.Feature2) AS Feature 2
    , IFNULL(row1.Feature3, row2.Feature3) AS Feature 3
FROM
    featureTable AS row1
    LEFT OUTER JOIN featureTable AS row2 ON row1.fallback = row2.familyroot
WHERE row1.userAgent LIKE '%Some User Agent String%'

So what this does, is takes your feature table, aliases it as row1 to get your specific model features. We then join it back to itself as row2 to get the family features. Then the ISNULL function says "if there is no Feature1 value in row 1 (it's null) then get the Feature1 value from row2".

Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜