简体   繁体   English

MySQL-从同一表的2行返回一行,用第二个“ override”的填充字段覆盖第一个“ default”的内容

[英]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.

相关问题 将数据从一个MYSQL表导入另一个MYSQL表 - 第一个表与第二个表的行数不同 - Importing data from one MYSQL table into another — the first table doesn't have the same amount of rows as second table MySQL:使用一个查询从第二个表返回行计数 - MySQL: Return row count from second table using one query mysql在同一张表中找到两行之间的差异,仅显示第二行的差异而不显示第一行 - mysql find the difference between two rows in same table and display only the second row difference not the first row MySQL select 来自一个表的行,第二个表中有多行,并在所选行中获取多行数组 - MySQL select row from one table with multiple rows in a second table and get array of multi row in selected row MySQL,如何从第一张表中选择一行,从第二张表中选择一行 - MySQL, How to select one row from first table and two from the second one 如何从一个表中返回一行,其中包含MySQL中第二个表中匹配id的数量? - How do I return a row from one table with the number of matching id's from a second table in MySQL? MySQL左连接第一和第二个结果,并作为一行返回 - MySQL left join first and second results and return as one row MySQL连接,从一个表返回1行,从另一个表返回多行作为数组或列表 - MySQL join, return 1 row from one table and multiple rows from another table as an array or list mysql join 2 table选择一个多行的字段 - mysql join 2 table to select one row fields with multiple rows 将一个表的多个行分配给第二个表的单元格(MySQL) - Assign multiple rows from one table to a cell in a second table (MySQL)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM