如何将两个表字段名称与mysql中的另一个值进行比较?

时间:2010-02-03 22:34:26

标签: php mysql

我有两张桌子

  1. table_school

    school_open_time|school_close_time|school_day
    8:00 AM         | 9:00PM          | Monday
    10:00 AM        | 7:00PM          | Wednesday
    
  2. table_college

     college_open_time|college_close_time|college_day    
     10:00 AM         | 8:00PM           | Monday
     10:00 AM         | 9:00PM           | Tuesday
     10:00 AM         | 5:00PM           | Wednesday
    
  3. 现在我想根据今天(意味着school_open_time)选择school_close time college_open_timecollege_close_timecollege_day=school_day=today,以及如果没有排在任何一个表格中的特定日期,它会显示空白字段(LEFT JOIN,我想我可以使用)。

    请为此建议我最佳和优化的查询。

    更新

    如果学校没有开放时间和休息时间,则必须返回college_open_time和college_close_time(不要填入数据库,只返回)作为school_open_time和school_close_time。并且在某一天必须有college_open_time和college_close_time


    我使用以下查询

     SELECT college_open_time,college_close_time ,school_open_time,
            school_close_time  FROM tbl_college
     LEFT JOIN tbl_school ON school_owner_id=college_owner_id 
     WHERE college_owner_id='".$_session['user_id']."' AND
     college_day='".date('l',time())."'";
    

    当table_school中没有给定日期的行时返回单行(左手有一些值,右手有空值),但是在左侧显示七行具有相同值的行(college_open_time,college_close_time)和6右侧的空白行(school_open_time和school_close_time)

    i need only one row when both table have a row of a given day
    

    但是使用上面的查询只占用相应table_school的第一行,其中school_owner_id是50(let),它没有看到应该给出school_day名称的条件


    更多更新@ 37Stars

    还有一点问题亲爱的, school_close_time和school_open时间的数据类型为TIME类型 而college_open_time和college_close_time的数据类型为VARCHAR类型。 我使用下面给出的代码,但我修改了一点,我接近结果,

    但是现在告诉我在下面的代码段中我必须写下IFNULL

    IFNULL(TIME_FORMAT())或TIME_FORMAT(IFNULL())

    SELECT TC.owner_id,college_open_time AS collegeOpen, 
           college_close_time AS collegeClose, 
    TIME_FORMAT(school_open_time, '%h:%i %p' ) AS schoolOpen,       
    TIME_FORMAT(school_close_time, '%h:%i %p' ) AS schoolClose
    FROM tbl_college TC
    LEFT JOIN tbl_school  TS ON TS.owner_id = TC.owner_id
    AND TC.college_day = TS.school_day
    WHERE college_day = DATE_FORMAT(NOW(),'%W')
    

    解决方案

    感谢37stars,你是天才,但感谢IFNULL,

    我写的是OPTIMUM AND BEST QUERY

    SELECT TC.owner_id,college_open_time AS collegeOpen,college_close_time AS 
        collegeClose, IFNULL(TIME_FORMAT(school_open_time, '%h:%i %p'),college_open_time) 
        AS schoolOpen,IFNULL(TIME_FORMAT(school_close_time, '%h:%i %p',college_close_time)
        AS schoolClose FROM tbl_college TC LEFT JOIN tbl_school TS 
        ON TS.owner_id = TC.owner_id AND TC.college_day = TS.school_day 
        WHERE college_day = DATE_FORMAT(NOW(),'%W') 
        FROM tbl_storecalendar TS LEFT JOIN tbl_delivery_hours TD 
        ON TD.store_id = TS.store_id 
        AND TD.del_day = TS.dayName WHERE dayName = DATE_FORMAT( NOW( ) , '%W' )
    

4 个答案:

答案 0 :(得分:4)

你想要一个FULL OUTER JOIN,但遗憾的是MySQL不支持这个。幸运的是,使用UNION ALL组合左连接和右连接有一种解决方法:

更新:更改了查询以回答OP的更新问题。

SELECT
    COALESCE(school_day, college_day) AS day,
    COALESCE(school_open_time, college_open_time) AS school_open_time,
    COALESCE(school_close_time, college_close_time) AS school_close_time,
    COALESCE(college_open_time, school_open_time) AS college_open_time,
    COALESCE(college_close_time, school_close_time) AS college_close_time
FROM (
    SELECT * FROM table_school LEFT JOIN table_college ON school_day = college_day
    UNION ALL
    SELECT * FROM table_school RIGHT JOIN table_college ON school_day = college_day
    WHERE school_day IS NULL
) AS T1

答案 1 :(得分:1)

我建议将数据库结构更改为具有以下结构的两个表:

table institutions:
institution | institution_id
table times:
institution_id | day | open_time | close_time

您可以轻松地将两个现有表格放入新时间表,即
INSERT INTO times(institution, day, open_time, close_time)
SELECT 'School', school_day, school_open_time, school_close_time
FROM table_school

然后获取查询结果很简单:
SELECT i.institution, t.open_time, t.close_time
FROM times t
LEFT JOIN institutions i on t.institution_id=i.institution_id
WHERE day='Wednesday'

答案 2 :(得分:1)

如果您想在一个查询中完成此操作,则必须使用UNION将结果放在一起,如下所示:

SELECT school_open_time AS openTime,`int` AS school_close_time
    FROM table_school WHERE school_day=DATE_FORMAT(NOW(),'%W')
UNION
SELECT college_open_time AS openTime,`int` AS college_close_time
    FROM table_college WHERE college_day=DATE_FORMAT(NOW(),'%W');

DATE_FORMAT(NOW(),'%W')转换为当周的当天。

然而,这种方法对我来说似乎并不那么好。首先,在缺少记录的情况下,它不会处理将NULL方法放入结果集中。您可以添加一些IF / ELSE语句来为您完成此操作,但说实话,我可能只是从您的PHP代码中进行两次单独的查询。这样,您可以专门处理每一个并从那里更容易地控制您的结果。

答案 3 :(得分:1)

您需要将 school_day = college_day 添加到您的JOIN子句中。

SELECT college_open_time, college_close_time, school_open_time, school_close_time
FROM dbo.tbl_college AS TC
    LEFT JOIN dbo.tbl_school AS TS ON TS.owner_id = TC.owner_id 
    AND TS.school_day = TC.college_day
WHERE TC.owner_id = 1 
    AND college_day = 'tuesday'