在表中输入高级SQL查询-不是唯一的表别名mysql


Inputting advanced SQL queries in table - not unique table alias mysql

我得到错误"不是唯一的表/别名",而试图显示数据从MySQL数据库到表。假设它与外键有关。

下面是创建表的SQL,然后插入一些值。SQL运行正常,没有出现任何问题,但我添加了代码,因为这一切都与在屏幕上显示表格密切相关。
CREATE TABLE city (
   id int not null primary key auto_increment,
   name varchar(30) not null
) type=innodb;
CREATE TABLE cinema (
   id int not null  primary key auto_increment,
   name varchar(50) not null,
   city int not null,
   foreign key(city) references city(id)
) type=innodb;
CREATE TABLE movie (
   id int not null primary key auto_increment,
   name varchar(30) not null
) type=innodb;
CREATE TABLE relationship (
   whattime time NOT NULL,
   whichdate date NOT NULL,
   movieid int not null,
   cinemaid int not null,
   primary key (cinemaid, movieid),
   foreign key(movieid) references movie(id),
   foreign key(cinemaid) references cinema(id)
) type=innodb;
INSERT INTO city (id, name) VALUES (1, 'Paris'), (2, 'Copenhagen'), (3, 'London'), (4, 'Lisbon') 
INSERT INTO movie (id, name) VALUES (1, 'The Church')
INSERT INTO cinema (id, name, city) VALUES (1, 'Pathé', 1), (2, 'Cinemaxx', 2), (3, 'Cineworld', 3), (4, 'ZON Lusomundo', 4)
INSERT INTO relationship (whattime, whichdate, movieid, cinemaid) VALUES ('21:00:00', '2011-12-27', 1, 1), ('19:30:00', '2011-12-28', 1, 2), ('20:00:00', '2011-12-27', 1, 3), ('21:00:00', '2012-01-02', 1, 4)

这是php,它给了我不是唯一的表/别名:'cinema'错误。知道为什么吗?

<?php
include "inc/mysql_con.php";
mysql_select_db($db) or die(mysql_error());
$query = "select city.name, cinema.name, movie.name, date, time from city, cinema, relationship, movie";
$query .= "where cinema.city = city.id";
$query .= "and cinemaid = cinema.id";
$query .= "and movieid = movie.name";
$query .= "order by date";
mysql_query($query) or die(mysql_error()); 
echo "<table id='premiere'>";
echo "<tr> <th>CITY</th> <th>CINEMA</th> <th>DATE</th> <th>TIME</th></tr>";
 while($result = mysql_fetch_array( $query )) {
    echo "<tr><td>"; 
    echo $result['city.name'];
    echo "</td><td>"; 
    echo $result['cinema.name'];
    echo "</td><td>";
    echo $result['date'];
    echo "</td><td>";
    echo $result['time'];
    echo "</td></tr>";
}
echo "</table>";    
?>

更新:现在我得到了正确的sql查询,下面的代码

SELECT city.name, cinema.name, whichdate, whattime
FROM city, cinema, relationship, movie
WHERE cinema.city = city.id
AND cinemaid = cinema.id
ORDER BY whichdate

在SQL中可以很好地显示表,但在PHP中不能:

错误:警告:mysql_fetch_array()期望参数1为resource, null,在content.php第75行
第75行:while($query = mysql_fetch_array($result)) {

看起来您的字符串连接缺少空格,因此前两行将给出…电影院,感情,电影院,哪里的电影院。City = City .id。在这种情况下,sql读取两次影院…事情会这么简单吗?在发送查询之前,我会回显查询。