join mysql输出中的两个表显示为null

qaxu7uf2  于 2021-06-23  发布在  Mysql
关注(0)|答案(3)|浏览(340)

我试图在mysql中连接两个表,但输出显示为null。
小贩

id vname        contactno    email                 address  status
1  raja         715487240  kobi.ram@hotmail.com   badulla    2
2  janarthan    77722222   jaranthan@gmail.com    Hali-ela   1

purchase

id   vendor_id    date         total     pay    due  payment_type

400     2        2018-10-08    10000    8000    2000     2
401     2        2018-10-12    12000    6000    6000     2

SQL查询

select
    v.vname, p.id, p.date, p.total, p.pay, p.due, p.payment_type
from purchase p, vendor v
WHERE p.vendor_id = v.id and date BETWEEN 2018-10-01 and 2018-10-31
lx0bsm1f

lx0bsm1f1#

我认为你应该用单引号来比较日期,只需对老式的join说再见,将join改成如下的新格式

SELECT v.vname,p.id,p.date,p.total,p.pay,p.due,p.payment_type 
FROM purchase p 
INNER JOIN vendor v ON p.vendor_id = v.id 
AND p.date BETWEEN '2018-10-01' and '2018-10-31'

小提琴:http://sqlfiddle.com/#!9/984801/1

vbkedwbf

vbkedwbf2#

单独使用join和where子句,在日期值中会丢失引号

select v.vname,p.id,p.date,p.total,p.pay,p.due,p.payment_type     
    from purchase p inner join vendor v on
    p.vendor_id = v.id  
    where p.date>= '2018-10-01' and  p.date <='2018-10-31'

http://sqlfiddle.com/#!9/4704eb/6号

hwazgwia

hwazgwia3#

我想这是因为日期时间格式
请在查询前添加日期格式

SET DATEFORMAT ymd;

相关问题