【问题标题】:How to get single value from second table if data exists else from first table?如果数据存在于第一个表中,如何从第二个表中获取单个值?
【发布时间】:2016-07-21 06:49:54
【问题描述】:

我有两张桌子。 我想从第一个表中获取最新的 updated_product_id 如果存在于第二个表中 else 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

上述查询的问题是如果第二个表中存在值则显示数据,否则返回空,我认为问题还在于 where 查询 输出: 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

【问题讨论】:

  • 您对此样本数据的预期结果是什么?
  • product_id = 4 如果数据存在于第二个表中 else product_id =1 如果第二个表为空
  • 需要澄清一下

标签: mysql


【解决方案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

【讨论】:

  • 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没有关系,
  • 好吧,我想我明白了。我没有意识到第二个表中的两个条目都属于第一个表中的同一行。此查询将为您获取最近更新的 id,如果没有更新,则为原始 id
  • 它返回多列,如果第二个表数据为空则不起作用。
  • 该查询不能返回多于一列,它只会返回id。当第二个表为空时,它返回 1 作为id,在答案中添加一个 sqlfiddle
  • 编辑:AS lastUpdate ON lastUpdate.client_product_id = sr_client_products.id 代替 AS lastUpdate ON lastUpdate.client_product_id = sr_client_products.client_product_id 工作正常。谢谢你。我会更新解决方案
【解决方案2】:

不确定你真正想做什么,但试试这个:

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

【讨论】:

  • 可以问你“coalesce”的功能是什么
  • @Charif coalesce
【解决方案3】:

我认为你需要一个简单的内部连接

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;

【讨论】:

  • 这行不通,因为您的代码用于连接两个表。
  • 如果两个表都包含相同的 product_id ,则 product_id 的联接工作。我根据您的解释推测了这一点
  • 我已经更新了答案..最终根据您的示例显示了预期的结果..
【解决方案4】:

问题在于你查询的 if 语句不会返回 '' 如果值没有退出它会返回空结果,所以使用 count(*) 它总是返回一个值,即使结果为空

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

【讨论】:

  • 它总是返回第一行限制,从哪里查询顺序不起作用。
  • 它应该返回什么?
  • product_id = 4 如果数据存在于第二个表中,否则 product_id =1 如果第二个表为空。您的查询有效,但唯一的问题是它返回的第一行和 where 子句 order by 和 limit 不起作用。
  • ProductUpdate.updated_date 不在select和limit中使用两个值limit 0,100试试吧
  • 我想告诉你有关 updated_date 的信息。查询中的where子句没有效果。
猜你喜欢
  • 2016-09-07
  • 1970-01-01
  • 2022-10-03
  • 2020-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-15
相关资源
最近更新 更多