简体   繁体   English

从左联接的第一个表获取变量

[英]Getting variable from first table in Left Join

So I have a basic question of how to get a variable from the a table of a outer join statement. 因此,我有一个基本问题,即如何从外部join语句的表中获取变量。

mysql_query("SELECT * 
               FROM (cal_events 
          left join cal_cities on cal_events.city_id = cal_cities.id) 
          left join cal_genre on cal_events.genre_id = cal_genre.id 
              WHERE start_day BETWEEN DATE_ADD(CURDATE(), INTERVAL -1 DAY) 
                                  AND DATE_ADD(CURDATE(), INTERVAL 5 DAY) 
           order by start_day");

while($info = mysql_fetch_array( $data )) {
  echo $info['id'];
}

This will echo out the genre's id, but I need the id from cal_events... using $info['cal_events.id'] just throws errors 这将回显流派的ID,但是我需要来自cal_events的ID ...使用$ info ['cal_events.id']会引发错误

HELP! 救命!

With pretty much all the interfaces to mysql any time you do a join and the tables have the same column names only the last occurence of that name is in the array. 每次进行连接时,几乎所有的mysql接口都可以使用,并且表具有相同的列名,只有该名称的最后一次出现在数组中。 To get arround this alias your columns: 要获取此别名的列,请执行以下操作:

SELECT cal_events.id as cal_events_id, cal_cities.* FROM (cal_events left join cal_cities on cal_events.city_id = cal_cities.id) left join cal_genre on cal_events.genre_id = cal_genre.id WHERE start_day BETWEEN DATE_ADD(CURDATE(), INTERVAL -1 DAY) AND DATE_ADD(CURDATE(), INTERVAL 5 DAY) order by start_day

Obviously that wont get all the columns in call i just ommited them because i dont know what they are. 显然,这不会使所有列都处于通话状态,我只是忽略了它们,因为我不知道它们是什么。 You should express them all individually and alias any that need to be aliased. 您应该分别表达所有内容,并为任何需要别名的内容加上别名。

   mysql_query("SELECT * , cal_events.id as cal_id
                       FROM (cal_events 
                  left join cal_cities on cal_events.city_id = cal_cities.id) 
                  left join cal_genre on cal_events.genre_id = cal_genre.id 
                      WHERE start_day BETWEEN DATE_ADD(CURDATE(), INTERVAL -1 DAY) 
                                          AND DATE_ADD(CURDATE(), INTERVAL 5 DAY) 
                   order by start_day");
    while($info = mysql_fetch_array( $data )) {
      echo $info['cal_id'];
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM