2016-07-21 122 views
0

我有两张桌子。 我想获取最新的updated_product_id,如果第二个表中存在其他product_id从第一个表中。 我第一台sr_client_products如果从第一个表中存在数据,如何从第二个表获取单个值?

id | product_id| client_id 
1 | 1   | 1 

和第二台sr_client_product_updates

id | product_id| updated_product_id | client_product_id| updated_date 
1 | 1   | 2     | 1    | 2016-02-03 
2 | 2   | 4     | 1    | 2016-06-07 

**预期输出:单排柱

updated_product_id = 4 as of updated_date 

,如果第二个表sr_client_product_updates没有值那么

product_id = 1 from first table 

** 直到现在查询

SELECT 
if(ProductUpdate.updated_product_id = '', 
(select ClientProduct.product_id from sr_client_products ClientProduct where ClientProduct.client_id=1), 
ProductUpdate.updated_product_id) 
as product_id 
from sr_client_product_updates ProductUpdate where ProductUpdate.client_product_id=1 order by ProductUpdate.updated_date DESC limit 1 

上述查询的问题在于它显示的数据,如果在第二个表中存在的值否则返回空的,我认为这个问题是与在查询还 输出: PRODUCT_ID = 4如果在第二表中存在数据 别的 PRODUCT_ID = 1,如果第二个表是空

被修改的查询:

SELECT 
if(count(*) = 0, 
(select ClientProduct.product_id from sr_client_products ClientProduct where ClientProduct.client_id=1), 
ProductUpdate.updated_product_id) 
as product_id 
from sr_client_product_updates ProductUpdate 
where ProductUpdate.client_product_id=1 
order by ProductUpdate.updated_date DESC limit 0,100 

解决方案,鉴于@Philipp

SELECT 
    COALESCE (
     sr_client_product_updates.updated_product_id, 
     sr_client_products.product_id 
    ) AS id 
FROM 
    sr_client_products 
LEFT JOIN (
    SELECT 
     MAX(updated_date) AS update_date, 
     client_product_id 
    FROM 
     sr_client_product_updates 
    GROUP BY 
     client_product_id 
) AS lastUpdate ON lastUpdate.client_product_id = sr_client_products.id 
LEFT JOIN sr_client_product_updates ON sr_client_product_updates.client_product_id = lastUpdate.client_product_id 
AND sr_client_product_updates.updated_date = lastUpdate.update_date where sr_client_products.id=1 
+0

,这是什么样的数据您预期的结果? – Blank

+0

的product_id = 4,如果在第二个表中存在的数据 否则 的product_id = 1,如果第二个表是空 –

+0

需要一些澄清 – JYoThI

回答

1
SELECT 
    COALESCE (
     sr_client_product_updates.updated_product_id, 
     sr_client_products.product_id 
    ) AS id 
FROM 
    sr_client_products 
LEFT JOIN (
    SELECT 
     MAX(updated_date) AS update_date, 
     client_product_id 
    FROM 
     sr_client_product_updates 
    GROUP BY 
     client_product_id 
) AS lastUpdate ON lastUpdate.client_product_id = sr_client_products.client_product_id 
LEFT JOIN sr_client_product_updates ON sr_client_product_updates.client_product_id = lastUpdate.client_product_id 
AND sr_client_product_updates.updated_date = lastUpdate.update_date 

返回已更新的product_id如果有一个对应于实际PRODUCT_ID。如果没有更新的ID,则返回product_id。

尽管我不确定哪些列需要用于连接,但product_id将是明显的选择,但您的示例使它有点奇怪。所以你可能需要修改ON条款。

Fiddle for example data

Fiddle for empty second table

+0

LEFT JOIN sr_client_product_updates ON sr_client_product_updates.client_product_id = sr_client_products.id 应该是这样的。并使用限制我需要添加选择语句,而不是sr_client_product_updates.client_product_id。与表格的product_Id没有关系, –

+0

好吧,我想我明白了。我没有意识到第二个表中的两个条目都属于第一个表中的同一行。如果没有更新,此查询将为您提供最近更新的ID或原始ID – Philipp

+0

如果第二个表数据为空,它将返回多个列并且不起作用。 –

0

不知道你的真正想做的事情,但试试这个:

select coalesce(t2.updated_product_id, t1.product_id) as product_id 
from sr_client_product_updates t1 
left join sr_client_products t2 
on t1.client_product_id = t2.client_id 
where t1.client_product_id = 1 
+0

可以是问你什么是funtion“合并” – Cherif

+0

这不会工作,如果第二个表是空 –

+0

@Charif [ 'coalesce'](https://www.google.com.tw/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwjm2MDHgYTOAhUBk5QKHaWzChcQFggcMAA&url=http%3A%2F%2Fwww.w3resource.com %2Fmysql%2Fcomparision-functions-and-operators%2Fcoalesce-function.php&usg = AFQjCNGu1CuFT46SRnF3i6RQh_UMYbefSg&sig2 = bf56zrECoYCMgB88UTO9Hg) – Blank

0

的问题是在if语句您查询将不会返回“ '如果该值不退出,则返回空结果,因此使用计数(*)即使结果为空时它总是返回一个值

SELECT 
    if(count(*) = 0, 
    (select ClientProduct.product_id from sr_client_products ClientProduct where ClientProduct.client_id=1), 
    ProductUpdate.updated_product_id) as product_id 
FROM 
    sr_client_product_updates ProductUpdate 
WHERE 
    ProductUpdate.client_product_id = 1 
ORDER BY 
    ProductUpdate.updated_date DESC limit 1 
+0

它总是返回第一行的限制,并且订单在where查询中不起作用。 –

+0

它应该返回什么? – Cherif

+0

product_id = 4如果数据存在于第二个表中,则其他product_id = 1如果第二个表为空。您的查询有效,但唯一的问题是其返回的第一行和where子句order by和limit不起作用。 –

0

我想你需要一个简单的内连接

SELECT sr_client_product_updates.updated_product_id 
FROM sr_client_products 
INNER JOIN sr_client_product_updates on 
     sr_client_product_updates.updated_product_id = sr_client_products.product_id; 
+0

由于您的代码用于连接两个表,因此这不起作用。 –

+0

如果两个表都包含相同的product_id,则为product_id工作的联接..这是我的解释 – scaisEdge

+0

对不起。我现在编辑了我的问题。 –

相关问题