无法使用子查询mysql获取日期值

时间:2018-03-21 19:01:17

标签: mysql

我有两个名为

的表

1表smartpos.pos_order_Id

+---------+--------------+---------+--------+--------------+----------------+------------+
| orderId | restaurantId | tableId | closed | customerName | customerNumber | dateorderd |
+---------+--------------+---------+--------+--------------+----------------+------------+
|       7 |           14 |       0 | yes    |              |                | 21/03/2018 |
|       8 |           14 |       0 | yes    |              |                | 21/03/2018 |
|       9 |           14 |       0 | no     |              |                | 20/03/2018 |
|      10 |           14 |       0 | yes    | soumya       | 1234567890     | 21/03/2018 |
|      11 |           14 |       0 | yes    |              |                | 21/03/2018 |
|      12 |           14 |       0 | yes    |              |                | 21/03/2018 |
|      13 |           14 |       0 | yes    |              |                | 21/03/2018 |
|      14 |           14 |       0 | yes    |              |                | 20/03/2018 |
|      15 |           14 |       0 | no     |              |                | 22/03/2018 |
+---------+--------------+---------+--------+--------------+----------------+------------+

2 smartpos.pos_invoice

   +---------------+---------+----------+-------------+-------------+------------+-------------+---------------+
| invoiceNumber | orderId | totalAmt | discountAmt | totalTaxAmt | grandTotal | paymentmode | paymentrefNum |
+---------------+---------+----------+-------------+-------------+------------+-------------+---------------+
|             1 |       7 |      200 |          34 |          46 |        212 | Cash        |               |
|             2 |      10 |     1200 |         200 |         280 |       1280 | Cash        |               |
|             3 |       1 |      720 |          34 |         120 |        806 | Cash        |               |
|             4 |      12 |      240 |          34 |          58 |        264 | Cash        |               |
|             5 |      13 |      330 |          32 |          83 |        381 | Cash        |               |
|             6 |      14 |       80 |           2 |          22 |        100 | Cash        |               |
+---------------+---------+----------+-------------+-------------+------------+-------------+---------------+

我想通过提供如下所示的restaurantId和日期,使用restaurantIdtwo dates来获取发票详细信息

select inv.invoiceNumber ,inv.totalAmt,inv.discountAmt,inv.totalTaxAmt,inv.grandTotal,i.dateorderd from smartpos.pos_invoice inv,smartpos.pos_order_Id i where inv.invoiceNumber in (select invv.invoiceNumber from  smartpos.pos_invoice invv where invv.orderId in(select ii.orderId from smartpos.pos_order_Id ii where ii.closed='yes' and ii.restaurantId=14 and STR_TO_DATE(dateorderd,'%d/%m/%Y') between  STR_TO_DATE('20/03/2018','%d/%m/%Y') and STR_TO_DATE('21/03/2018','%d/%m/%Y'))) group by inv.invoiceNumber ;

out put:

+---------------+----------+-------------+-------------+------------+------------+
| invoiceNumber | totalAmt | discountAmt | totalTaxAmt | grandTotal | dateorderd |
+---------------+----------+-------------+-------------+------------+------------+
|             1 |      200 |          34 |          46 |        212 | NULL       |
|             2 |     1200 |         200 |         280 |       1280 | NULL       |
|             4 |      240 |          34 |          58 |        264 | NULL       |
|             5 |      330 |          32 |          83 |        381 | NULL       |
|             6 |       80 |           2 |          22 |        100 | NULL       |
+---------------+----------+-------------+-------------+------------+------------+

但是当我运行上面的查询时,它会给出空值,如何获取日期?

1 个答案:

答案 0 :(得分:0)

很难理解你写的内容,但我觉得你的查询比看起来简单得多,试着用这种方法:

select 
    inv.invoiceNumber ,
    inv.totalAmt,
    inv.discountAmt,
    inv.totalTaxAmt,
    inv.grandTotal,
    i.dateorderd 
from 
    smartpos.pos_invoice inv,
    smartpos.pos_order_Id i 
where 
    inv.orderId = i.orderId
and 
    i.closed='yes' 
and 
    i.restaurantId=14
and 
    STR_TO_DATE(dateorderd,'%d/%m/%Y') between STR_TO_DATE('20/03/2018','%d/%m/%Y') and STR_TO_DATE('21/03/2018','%d/%m/%Y')
group by 
    inv.invoiceNumber;