简体   繁体   English

PHP&SQL问题:当它不返回某些字段时,需要回显所有可能的结果

[英]PHP & SQL issue: Need to echo all results possible when it doesn't return some fields

Is there a way to change this sql statement to return results it does find? 有没有一种方法可以更改此sql语句以返回确实找到的结果? Basically right now orderprc.item_no and orderprc.cust_no do not always exist in the table. 基本上现在, orderprc.item_noorderprc.cust_no并不总是存在于表中。 So if they don't my query does not return anything. 因此,如果不这样做,我的查询将不会返回任何内容。

What I want to do when they don't exist is echo everything it does find, but just leave the rest blank. 当它们不存在时,我想做的是回显它确实找到的所有内容,而将其余部分留空。 I am echoing these inside a table. 我在桌子里回荡这些。 Any suggestions? 有什么建议么? Let me know if you need more info. 让我知道您是否需要更多信息。

SELECT 
    ordernumber.ord_num, 
    lineitem.item_no, 
    lineitem.item_desc_1,
    ordernumber.cus_no, 
    lineitem.unit_price, 
    cic.sman, 
    orderprc.description
FROM ordernumber JOIN lineitem
ON ordernumber.ord_num = lineitem.ord_num
JOIN cicmpy ON ordernumber.cus_no = cic.debcode
JOIN orderprc ON ordernumber.cus_no = orderprc.cust_no
WHERE ordernumber.ord_num = $multi_orders
  AND orderprc.item_no = lineitem.item_no
  AND orderprc.cust_no = ordernumber.cus_no
  AND getdate() between start_dt and end_dt

Yes, use an OUTER JOIN (spelled out in SQL statements as either LEFT JOIN or RIGHT JOIN ) as opposed to an INNER JOIN. 是的,请使用OUTER JOIN(在SQL语句中拼写为LEFT JOINRIGHT JOIN ),而不要使用INNER JOIN。 When you just use the term JOIN , it defaults to an INNER JOIN. 当您仅使用术语JOIN ,它默认为INNER JOIN。

In your example, the SQL statement would look like: 在您的示例中,SQL语句如下所示:

SELECT ordernumber.ord_num, lineitem.item_no, lineitem.item_desc_1,
    ordernumber.cus_no, lineitem.unit_price, cic.sman, orderprc.description
FROM ordernumber 
    JOIN lineitem ON ordernumber.ord_num = lineitem.ord_num
        JOIN cicmpy ON ordernumber.cus_no = cic.debcode
            LEFT JOIN orderprc ON ordernumber.cus_no = orderprc.cust_no
WHERE ordernumber.ord_num = $multi_orders
    AND orderprc.item_no = lineitem.item_no
    AND orderprc.cust_no = ordernumber.cus_no
    AND getdate() BETWEEN start_dt AND end_dt

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

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