[英]Select results from one table and for each row select all rows that have the same id from another table
[英]Select field from table only when all rows paired to same ID have a date
我正在尝试从数据库中检索一些信息。
我有下表
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
有些订单与多个项目配对。 当项目为2时,我要自动添加一个发货日期(该功能可以正常使用)。 但是,仅当所有其余项目都附有日期时,才需要添加脚本。
例如:->订单号'1050':项目6有日期,但项目1没有日期,因此我们不会在2中添加发货日期->订单号'2000':那里只有项目2,所以我们很好,发货日期将被添加->订单号'2006':项目1有日期,因此我们也可以在项目2中添加日期。
换句话说,如果订单中的商品不是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
因此,在上面的查询中,我传递了订单号(例如'1050'),我想以某种方式检查所有其他商品(2以外)是否都具有发货日期。 如果它们全部都进行了,如果没有,则什么也不做。
我尝试了不同的查询,但没有任何效果。 只能使用mysql查询吗? 还是也需要一些PHP代码?
如果有什么可以帮助我或者至少将我引向正确的道路,我将不胜感激。
这样的事情应该可以帮助您:
$sql = "Select orderNo from orders where item != '2' AND shipping > '2000-01-01'";
选择:
$sql = "Select orderNo from orders where item != '2' AND shipping > '0'";
一种易于理解的方法是查询:
SELECT orderNo FROM orders WHERE orderNo = '".$orderNo."' AND item !=2 AND shipping>'0000:00:00'
然后运行php num行,以查看有多少行与您的查询匹配。 如果结果大于0,则表示您不应继续
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.