简体   繁体   English

MySQL:如何从另一个表的列中选择数据

[英]MySQL: How to select data from a column in another table

I have the following select statement: 我有以下选择语句:

  SELECT
    items.id,
    items.name AS item_name,
    items.tobuy,
    items.list_id,
    items.note,
    items.unit,
    MIN(NULLIF(packages.ppu, 0)) AS mppu,
    packages.price AS mprice,
    items._deleted
  FROM
    items
  INNER JOIN
    lists ON lists.id = items.list_id
  LEFT JOIN
    packages ON items.id = packages.item_id
  WHERE
    lists.user_id = 1 AND
    items._deleted = '0'
  GROUP BY
    items.id
  ORDER BY
    tobuy DESC,
    item_name

But what I really want is the price to come from the package that has the minimum ppu (which is not necessarily the package with the minimum price). 但是我真正想要的是价格来自具有最小ppu的包装(不一定是具有最小价格的包装)。

Any ideas? 有任何想法吗?

Sample Records: 样本记录:

Table: items: 表:项目:

id,  name,  tobuy, list_id, note, unit, _deleted
95,  test1, 1,     1,       null, null, 0
69,  test2, 1,     1,       null, null, 0
194, test3, 1,     1,       null, null, 0
162, test4, 1,     1,       null, null, 0

Table: lists: 表:列表:

id, name,  user_id
1,  list1, 1

Table: packages: 表:包:

id,  item_id, price, ppu
392, 95,      0,     0
117, 95,      13.49, 0.078
391, 95,      0,     0
386, 69,      0,     0
387, 69,      0,     0
388, 69,      0,     0
368, 194,     4.58,  0.138
18,  194,     3.38,  0.177
17,  194,     3.88,  0.144

The results should be four items with the following information: 结果应为包含以下信息的四个项目:

id,  item_name,  tobuy, list_id, note, unit, mppu,  mprice, _deleted
95,  test1,      1,     1,       null, null, 0.078, 13.49,  0
69,  test2,      1,     1,       null, null, 0,     0,      0
194, test3,      1,     1,       null, null, 0.138, 4.58,   0
162, test4,      1,     1,       null, null, 0,     0,      0

Notice that item 162 doesn't have any corresponding packages, but it still shows up in the list. 请注意,项目162没有任何相应的程序包,但仍显示在列表中。 This is the reason for the "LEFT JOIN" 这就是“ LEFT JOIN”的原因

BTW, "mppu" stands for "minimum price per unit" 顺便说一句,“ mppu”代表“单位最低价格”

can you try for the 2nd left: packages ON ( packages.item_id = items.id and select min(packages.ppu) from packages where item_id=item.id) 您可以尝试左移第二个吗: packages ON ( packages.item_id = items.id and select min(packages.ppu) from packages where item_id=item.id)

also make sure to change group by to items.id not tested 还请确保将分组依据更改为未经测试的items.id

With mysql this is surprisingly simplex. 使用mysql,这是令人惊讶的单工。

Order by ppn and move the group by to an outer query 按ppn排序,然后将组移动到外部查询

SELECT * FROM (
  SELECT
    items.id,
    items.name AS item_name,
    items.tobuy,
    items.list_id,
    items.note,
    items.unit,
    MIN(NULLIF(packages.ppu, 0)) AS mppu,
    packages.price AS mprice,
    items._deleted
  FROM items
  INNER JOIN lists
    ON lists.id = items.list_id
    AND lists.user_id = 1
  LEFT JOIN packages
    ON items.id = packages.item_id
  WHERE items._deleted = '0'
  ORDER BY
    ppm,
    tobuy DESC,
    item_name) x
GROUP BY
  id

The following works to get the results that I was looking for. 以下工作可以得到我想要的结果。

SELECT * FROM (  
  SELECT
    items.id,
    items.name AS item_name,
    items.tobuy,
    items.list_id,
    items.note,
    items.unit,
    NULLIF(packages.ppu, 0) AS mppu,
    packages.price AS mprice,
    items._deleted
  FROM
    items
  INNER JOIN
    lists ON lists.id = items.list_id
    AND lists.user_id = 1 
  LEFT JOIN
    packages ON packages.item_id = items.id
  WHERE
    items._deleted = '0'
  ORDER BY
    tobuy DESC,
    item_name,
    IFNULL(mppu, 999999)) x
GROUP BY
  x.id
ORDER BY
  tobuy DESC,
  item_name

Thanks to everyone for the input to get me through this. 感谢大家提供的帮助,以帮助我完成此任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM