简体   繁体   English

仅当与相同ID配对的所有行都有日期时,才从表中选择字段

[英]Select field from table only when all rows paired to same ID have a date

I am trying to retrieve some info from the DB. 我正在尝试从数据库中检索一些信息。

I have the following table 我有下表

orderNo   item   shipping
 1050      1     0000-00-00    
 1050      2     0000-00-00
 1050      6     2016-09-18
 2000      2     0000-00-00
 2006      1     2016-09-05
 2006      2     0000-00-00

There are some orders paired with multiple items. 有些订单与多个项目配对。 When the item is 2, I want to automatically add a shipping date (that functionality works fine). 当项目为2时,我要自动添加一个发货日期(该功能可以正常使用)。 But the script will need to be added ONLY when all the rest items has a date attached to them. 但是,仅当所有其余项目都附有日期时,才需要添加脚本。

For example: -> order number '1050': Item 6 has a date but Item 1 has no date, so we won't add shipping date to 2 -> order number '2000': There is only item 2 there, so we are good to go, shipping date will be added -> order number '2006': Item 1 has a date, so again, we can add date to item 2 as well. 例如:->订单号'1050':项目6有日期,但项目1没有日期,因此我们不会在2中添加发货日期->订单号'2000':那里只有项目2,所以我们很好,发货日期将被添加->订单号'2006':项目1有日期,因此我们也可以在项目2中添加日期。

In other words, when the order has an item other than 2, and at least one of the other paired items has no shipping date then I want to ignore it 换句话说,如果订单中的商品不是2,而其他配对商品中至少有一个没有发货日期,那么我想忽略它

//adding the shipping date to item 2
$sql = "UPDATE orders 
        SET shipping = '".$shipping."'
        WHERE orderNo = '".$orderNo."' AND item = '2'";
Db::getInstance()->query($sql);

//add some kind of query to check whether shipping of rest items is not zero. If it is '0000-00-00' then do not proceed
SELECT orderNo FROM orders WHERE orderNo = '".$orderNo."' AND shipping in other items than 2 has a date
      //If query returns true then proceed and execute the following
      $new_payment = new itemPayment();
      //else do nothing

So in the above query, I am passing the order number (eg. '1050') and I somehow want to check if all the other items (other than 2) has a shipping date. 因此,在上面的查询中,我传递了订单号(例如'1050'),我想以某种方式检查所有其他商品(2以外)是否都具有发货日期。 If they all have then proceed, if not then do nothing. 如果它们全部都进行了,如果没有,则什么也不做。

I tried different queries but nothing worked. 我尝试了不同的查询,但没有任何效果。 Can it be done only with mysql queries? 只能使用mysql查询吗? or does it need some PHP code too? 还是也需要一些PHP代码?

would really appreciate if anything can help me with that or at least lead me to the right path. 如果有什么可以帮助我或者至少将我引向正确的道路,我将不胜感激。

Something like this should help you: 这样的事情应该可以帮助您:

$sql = "Select orderNo from orders where item != '2' AND shipping > '2000-01-01'";

Alternative: 选择:

$sql = "Select orderNo from orders where item != '2' AND shipping > '0'";

An understandable way to do this, is going for a query: 一种易于理解的方法是查询:

SELECT orderNo FROM orders WHERE orderNo = '".$orderNo."' AND item !=2 AND shipping>'0000:00:00'

Then run php num rows to see how many rows match to your query. 然后运行php num行,以查看有多少行与您的查询匹配。 If the result is more than 0, that means you should not proceed 如果结果大于0,则表示您不应继续

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

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