我无法从产品表中提取所有记录的查询

时间:2018-09-25 06:52:41

标签: mysql

我需要它来拉出产品表中的所有产品,似乎只有在快速库存调查行中有相应数据时,才拉出产品记录。

SELECT from_unixtime(j.job_datetime/1000+7200, '%d/%m/%Y %H:%i') AS tme,
  s.store_name,
  s.store_chain,
  dev.device_user_name,
  qss.quick_stock_survey_job_id,
  qss.quick_stock_survey_id,
  qss_lines.quick_stock_survey_lines_quantity,
  qss_lines.quick_stock_survey_lines_is_outofstock,
  prd.product_description,
  prd.product_order,
  cat.category_name,
  product_status
FROM product AS prd
LEFT JOIN quick_stock_survey_lines AS qss_lines
  ON qss_lines.quick_stock_survey_lines_product_id = prd.product_id
LEFT JOIN quick_stock_survey AS qss
  ON qss.quick_stock_survey_id = qss_lines.quick_stock_survey_lines_quick_stock_survey_id
LEFT JOIN job AS j
  ON j.job_id = qss.quick_stock_survey_job_id
LEFT JOIN store AS s 
  ON  j.job_store_id = s.store_id 
LEFT JOIN device_user AS dev
  ON dev.device_user_id = j.job_device_user_id
LEFT JOIN category AS cat
  ON cat.category_id = prd.product_default_category_id
WHERE dev.device_user_client_id = 173
  AND j.job_type = 6
  AND ((j.job_datetime/1000) BETWEEN (UNIX_TIMESTAMP()-604800) AND UNIX_TIMESTAMP()) 
ORDER BY prd.product_order, dev.device_user_name;

2 个答案:

答案 0 :(得分:0)

我建议将WHERE子句的逻辑移到相应的ON子句。原因是您当前的WHERE子句冒着过早从结果集中过滤掉整个记录的风险,这仅仅是因为许多联接条件中的某些条件可能不匹配。

SELECT
    FROM_UNIXTIME(j.job_datetime/1000+7200, '%d/%m/%Y %H:%i') AS tme,
    s.store_name,
    s.store_chain,
    dev.device_user_name,
    qss.quick_stock_survey_job_id,
    qss.quick_stock_survey_id,
    qss_lines.quick_stock_survey_lines_quantity,
    qss_lines.quick_stock_survey_lines_is_outofstock,
    prd.product_description,
    prd.product_order,
    cat.category_name,
    product_status
FROM product as prd
LEFT JOIN quick_stock_survey_lines AS qss_lines
    ON qss_lines.quick_stock_survey_lines_product_id = prd.product_id
LEFT JOIN quick_stock_survey AS qss
    ON qss.quick_stock_survey_id = qss_lines.quick_stock_survey_lines_quick_stock_survey_id
LEFT JOIN job AS j
    ON j.job_id = qss.quick_stock_survey_job_id AND
       j.job_type = 6 AND
       ((j.job_datetime/1000) BETWEEN (UNIX_TIMESTAMP()-604800) AND
           UNIX_TIMESTAMP())
LEFT JOIN store AS s 
    ON j.job_store_id = s.store_id
LEFT JOIN device_user AS dev
    ON dev.device_user_id = j.job_device_user_id AND
       dev.device_user_client_id = 173
LEFT JOIN category AS cat
    ON cat.category_id = prd.product_default_category_id
ORDER BY
    prd.product_order,
    dev.device_user_name;

答案 1 :(得分:0)

只需删除您的WHERE子句即可过滤掉数据,然后您就可以提取所有产品

SELECT from_unixtime(j.job_datetime/1000+7200, '%d/%m/%Y %H:%i') AS tme,
s.store_name,
s.store_chain,
dev.device_user_name,
qss.quick_stock_survey_job_id,
qss.quick_stock_survey_id,
qss_lines.quick_stock_survey_lines_quantity,
qss_lines.quick_stock_survey_lines_is_outofstock,
prd.product_description,
prd.product_order,
cat.category_name,
product_status
FROM product as prd
LEFT  JOIN quick_stock_survey_lines as qss_lines
On qss_lines.quick_stock_survey_lines_product_id = prd.product_id
LEFT JOIN quick_stock_survey as qss
ON qss.quick_stock_survey_id =qss_lines.quick_stock_survey_lines_quick_stock_survey_id
LEFT JOIN job as j
On j.job_id = qss.quick_stock_survey_job_id
LEFT JOIN store as s 
On  j.job_store_id = s.store_id 
LEFT JOIN device_user as dev
On dev.device_user_id = j.job_device_user_id
LEFT JOIN category as cat
On cat.category_id = prd.product_default_category_id
Order BY prd.product_order, dev.device_user_name;