Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从两个表中查询数据_Php_Mysql - Fatal编程技术网

Php 从两个表中查询数据

Php 从两个表中查询数据,php,mysql,Php,Mysql,我有两张桌子: 用户详细信息:id,名字,姓氏 用户计时器:id,用户id(在用户详细信息中按id引用),签入日期,签入时间 现在,当用户输入开始和结束日期时,我希望生成结果: 当用户输入我希望输出的日期时,如下所示: 名字,姓氏,入住日期,入住时间 我已经执行了获取签入日期和签入时间的查询: select * from user_timer where `checkin_date` between '$startdate' and '$enddate' 现在,如何使用连接查询或子查询获取fi

我有两张桌子:

  • 用户详细信息:id名字姓氏
  • 用户计时器:id用户id(在用户详细信息中按id引用),签入日期签入时间
  • 现在,当用户输入开始和结束日期时,我希望生成结果:

    当用户输入我希望输出的日期时,如下所示:

    名字姓氏入住日期入住时间

    我已经执行了获取签入日期和签入时间的查询:

    select * from user_timer where `checkin_date` between '$startdate' and '$enddate'
    
    现在,如何使用连接查询或子查询获取firstname&lastname

    我只想对输出使用一个查询。请给我一个建议,我是PHP新手。

    试试这个:-

    select * from user_timer u,user_details ud where ud.checkin_date between '$startdate' and '$enddate' and u.id = ud.id
    

    像这样的怎么样

    select  ud.firstname,
            ud.lastname, 
            ut.checkin-date, 
            ut.checkin-time
    from    user_timer ut INNER JOIN
            user_details ud ON  ut.user_id = ud.id
    where   checkin_date between '$startdate' and '$enddate'
    
    我会怎么做:

    select ut.firstname, ut.lastname, ud.checkin_date, ud.checkin_time from user_timer ut LEFT JOIN user_details ud ON ud.id=ut.user_id  where ut.checkin_date between '$startdate' and '$enddate'
    
    试试这个

    select utimer.checkin-date, utimer.checkin-time,  user.firstname, user.lastname
    from user_timer utimer
    INNER JOIN user_details user ON  utimer.user_id = user.id
    where  utimer.checkin_date between '$startdate' and '$enddate'
    

    谢谢你,在给了我一个答案却没有解释原因将近三年后,谁投了我的票。干得好!
    select utimer.checkin-date, utimer.checkin-time,  user.firstname, user.lastname
    from user_timer utimer
    INNER JOIN user_details user ON  utimer.user_id = user.id
    where  utimer.checkin_date between '$startdate' and '$enddate'