[英]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). 我正在尝试使用http://wurfl.sourceforge.net/smart.php上 WUFL数据库中的移动设备查找数据,但是我在理解所需的MySQL代码时遇到了问题(我将Coldfusion用于服务器后端)。 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. WUFL以XML格式提供(到目前为止大约有15200条记录),我编写了将数据保存到MySQL数据库的方法。 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; 基本上,它是这样工作的:首先,使用来自CGI提取的userAgent数据运行选择,以使用LIKE与已知的移动设备(第1行)匹配; if found then use the resultant fallback field to look up the default data for the mobile device's 'family root' (row 2). 如果找到,则使用结果后备字段查找移动设备的“家庭根”(第2行)的默认数据。 The two rows need to be combined by overwriting the contents of (row 2) with the specific mobile device's features of (row 1). 需要通过用(行1)的特定移动设备功能覆盖(行2)的内容来合并两行。 Both rows contain NULL entries and not all the features are present in (row 1). 两行均包含NULL条目,并且(第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. 我希望这是有道理的,我将提供我认为SQL的外观,但我可能会更加困惑。
Really appreciate any assistance! 非常感谢您的协助!
This would be my shot at it in SQL Server. 这将是我在SQL Server中的镜头。 You would need to use IFNULL
instead of ISNULL
: 您将需要使用IFNULL
而不是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: 这应该在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. 因此,这就是将特征表作为别名,并将其命名为row1以获取特定的模型特征。 We then join it back to itself as row2 to get the family features. 然后,我们将其作为row2加入其自身,以获取家庭功能。 Then the ISNULL
function says "if there is no Feature1 value in row 1 (it's null) then get the Feature1 value from row2". 然后, ISNULL
函数说:“如果第1行中没有Feature1值(它为null),则从第2行中获取Feature1值”。
Hope that helps. 希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.