MYSQL在内部选择中使用外部选择值

时间:2017-11-22 16:11:12

标签: php mysql join

我知道这个问题经常问,但我没有找到任何答案(或者说不可能):

我得到了这个查询


SELECT
  `items`.`id`,
  `items`.`part_number`,
  `item_categories`.`name` AS category,
  `suppliers`.`name` AS supplier,
  `items`.`supplier_id`,
  `items`.`name`,
  `items`.`inventory`,
  `items`.`package_items`,
  `items`.`order_step`,
  `items`.`price`,
  `items`.`discount`,
  `items`.`scale`,
  `items`.`by_scale`,
  `items`.`has_tax`,
  `items`.`category_id`,
  `items`.`enable`,
  orders.last_orders_amount
FROM
  `items`
INNER JOIN
  `suppliers` ON `items`.`supplier_id` = `suppliers`.`id`
INNER JOIN
  `item_categories` ON `items`.`category_id` = `item_categories`.`id`
INNER JOIN (
    SELECT 
GROUP_CONCAT(
    JSON_EXTRACT(
      `orders`.`items`,
      CONCAT('$."',
      "3",
      '".amount')
    )
  ORDER BY
    `orders`.`createDate` DESC
  ) AS last_orders_amount
FROM
  `orders`
WHERE
  JSON_EXTRACT(`orders`.`items`,
  '$."3"') IS NOT NULL
LIMIT 4
) orders ON orders.last_orders_amount IS NOT NULL
WHERE
  1

基本上我想得到所有'项目'最后4次出现在' order'基于itemid

我需要将该查询中的 3 数字替换为来自外部联接的itemid

(我知道有INNER JOIN导致只获得订单中出现的物品)

0 个答案:

没有答案