Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.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 使用左连接选择MySQL_Php_Mysql_Left Join - Fatal编程技术网

Php 使用左连接选择MySQL

Php 使用左连接选择MySQL,php,mysql,left-join,Php,Mysql,Left Join,我正在尝试连接两个表并从“railstp”中进行选择。我简化了表格 表“railstp”如下所示 count startdate startlocation atoc 1 2013-09-28 lester a 2 2013-09-28 nottm a 3 2013-09-20 lester a 4 2013-09-28 birm

我正在尝试连接两个表并从“railstp”中进行选择。我简化了表格

表“railstp”如下所示

count  startdate   startlocation    atoc

  1    2013-09-28     lester          a
  2    2013-09-28     nottm           a
  3    2013-09-20     lester          a   
  4    2013-09-28     birm            a 
count  startlocation  goodlocation

  1      lester         Leicester
  2      nottm          Nottingham
表“位置”如下所示

count  startdate   startlocation    atoc

  1    2013-09-28     lester          a
  2    2013-09-28     nottm           a
  3    2013-09-20     lester          a   
  4    2013-09-28     birm            a 
count  startlocation  goodlocation

  1      lester         Leicester
  2      nottm          Nottingham
我试图从“位置”表中获取“正确”(良好位置)描述,以替换缩写描述(位置),并选择所有起始日期为2013-09-28的位置。如果没有一个“适当的”描述,如果我只显示缩写描述就好了

因此,我希望实现的结果是

2013-09-28    Leicester    a
2013-09-28    Nottingham   a
2013-09-28    birm         a
我的代码如下:-

require('connect_db.php');
mysqli_select_db($mysql_link,"Timetable");
function show_records($mysql_link)
{
$q="SELECT railstp.startdate,railstp.startlocation,railstp.atoc,location.startlocation,location.goodlocation
FROM railstp
LEFT JOIN location
ON railstp.startlocation=location.startlocation
WHERE railstp.startdate='2013-09-28'
ORDER BY startdate";

$r=mysqli_query($mysql_link,$q);

if ($r)
{
echo "<Table id='customers'>
<tr>
<th>From</th>
<th>Departing</th>
<th>ATOC</th>
</tr>";

while ($row=mysqli_fetch_array($r,MYSQLI_ASSOC))
{
echo "<tr>";
echo "<td>".$row['startdate']."</td>";
echo "<td>".$row['startlocation']."</td>";
echo "<td>".$row['atoc']."</td>";
echo "</tr>";
}
echo "</Table>";
}
else {echo '<p>'.mysqli_error($mysql_link).'</p>' ;}
}
show_records($mysql_link);
mysqli_close($mysql_link);
require('connect_db.php');
mysqli_select_db($mysql_链接,“时间表”);
函数show_记录($mysql_链接)
{
$q=“选择railstp.startdate、railstp.startlocation、railstp.atoc、location.startlocation、location.goodlocation
来自railstp
左连接位置
在Railst上stp.startlocation=location.startlocation
其中Railst.startdate='2013-09-28'
按开始日期订购”;
$r=mysqli\u查询($mysql\u链接,$q);
如果($r)
{
回声“
从…起
离开
阿托克
";
而($row=mysqli\u fetch\u数组($r,mysqli\u ASSOC))
{
回声“;
回显“$row['startdate']”;
回显“$row['startlocation']”;
回显“$row['atoc']”;
回声“;
}
回声“;
}
else{echo''.mysqli_错误($mysql_链接)。'

'} } 显示_记录($mysql_链接); mysqli_close($mysql_link);
该选择没有显示“正确”(好位置)的描述-即它应该显示诺丁汉而不是诺丁汉,莱斯特而不是莱斯特


感谢您的帮助

您的查询中有两列名称相同。没有办法在php代码中判断您想要哪一个,因此选择了第一个。使用
作为
重命名其中一个:

SELECT rs.startdate, rs.startlocation as shortstartlocation, rs.atoc,
       coalesce(l.startlocation,  rs.startlocation) as startlocation,
       l.goodlocation
FROM railstp rs LEFT JOIN
     location l
      ON rs.startlocation = l.startlocation
WHERE rs.startdate = '2013-09-28'
ORDER BY startdate

查询中有两个同名的列。没有办法在php代码中判断您想要哪一个,因此选择了第一个。使用
作为
重命名其中一个:

SELECT rs.startdate, rs.startlocation as shortstartlocation, rs.atoc,
       coalesce(l.startlocation,  rs.startlocation) as startlocation,
       l.goodlocation
FROM railstp rs LEFT JOIN
     location l
      ON rs.startlocation = l.startlocation
WHERE rs.startdate = '2013-09-28'
ORDER BY startdate

为此,可以使用SQL函数
COALESCE()

此函数在其参数中使用第一个非null值

SELECT railstp.startdate,
       COALESCE(location.goodlocation, railstp.startlocation) as startloc,
       railstp.atoc
FROM railstp
LEFT JOIN location
ON railstp.startlocation=location.startlocation
WHERE railstp.startdate='2013-09-28'
ORDER BY startdate
所以这里,如果
location.goodlocation
为空,则使用
railstp.startlocation

更新

如果要添加
结束位置
,可以再次加入位置表。 我要指出的是,为了让事情变得更简单,我添加了表别名来区分不同的表

SELECT r.startdate,
       COALESCE(l1.goodlocation, r.startlocation) as startloc,
       COALESCE(l2.goodlocation, r.endlocation) as endloc,
       r.atoc
FROM railstp r
LEFT JOIN location l1
ON r.startlocation = l1.startlocation
LEFT JOIN location l2
ON r.endlocation = l2.startlocation
WHERE r.startdate='2013-09-28'
ORDER BY r.startdate

当然,如果起始位置和结束位置都使用了
位置
表,则可以使用SQL函数
COALESCE()

此函数在其参数中使用第一个非null值

SELECT railstp.startdate,
       COALESCE(location.goodlocation, railstp.startlocation) as startloc,
       railstp.atoc
FROM railstp
LEFT JOIN location
ON railstp.startlocation=location.startlocation
WHERE railstp.startdate='2013-09-28'
ORDER BY startdate
所以这里,如果
location.goodlocation
为空,则使用
railstp.startlocation

更新

如果要添加
结束位置
,可以再次加入位置表。 我要指出的是,为了让事情变得更简单,我添加了表别名来区分不同的表

SELECT r.startdate,
       COALESCE(l1.goodlocation, r.startlocation) as startloc,
       COALESCE(l2.goodlocation, r.endlocation) as endloc,
       r.atoc
FROM railstp r
LEFT JOIN location l1
ON r.startlocation = l1.startlocation
LEFT JOIN location l2
ON r.endlocation = l2.startlocation
WHERE r.startdate='2013-09-28'
ORDER BY r.startdate

这个,当然,,如果
位置
表同时用于起始位置和结束位置。

谢谢Gordon-我是否需要回显shortstartlocation,因为我正在努力使它工作?谢谢Gordon-我是否需要回显shortstartlocation,因为我正在努力使它工作?如果我想在railstp中添加一个名为endlocation的新列,但要查找位置表中相同的“正确”描述我该怎么做?非常感谢。如果我想在railstp中添加一个名为endlocation的新列,但从location表中查找相同的“正确”描述,我该怎么做?非常感谢。