Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.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_Left Join - Fatal编程技术网

Php 左连接-返回多个结果

Php 左连接-返回多个结果,php,mysql,left-join,Php,Mysql,Left Join,我有一个挑战,左路的加入给了我很多结果。我想列出比赛的优胜者名单。在我的优胜者表中,我有id、竞赛、图像、位置、发布。我很感激能得到的任何帮助- $id返回当前竞赛id 在我的示例中,winners表只有3行,但返回9行 我的问题是: $sql = "SELECT photocontest_winners.id, photocontest_winners.contest, photocontest_winners.im

我有一个挑战,左路的加入给了我很多结果。我想列出比赛的优胜者名单。在我的优胜者表中,我有id、竞赛、图像、位置、发布。我很感激能得到的任何帮助-

$id返回当前竞赛id

在我的示例中,winners表只有3行,但返回9行

我的问题是:

$sql = "SELECT 
            photocontest_winners.id, 
            photocontest_winners.contest, 
            photocontest_winners.image, 
            photocontest_winners.place, 

            photocontest_entries.id, 
            photocontest_entries.contest, 
            photocontest_entries.name, 
            photocontest_entries.title,
            photocontest_entries.image

        FROM photocontest_winners 

        LEFT JOIN photocontest_entries ON photocontest_winners.contest = photocontest_entries.contest

        WHERE photocontest_winners.contest = $id AND photocontest_winners.published = 1 
        ORDER BY photocontest_winners.place";

$result = $conn->query($sql);

while($row = $result->fetch_assoc()) {

    echo("<pre>");
    print_r($row);
    echo("</pre>");

}
带有样本数据的竞赛表:

SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
--  Table structure for `photocontest_entries`
-- ----------------------------
DROP TABLE IF EXISTS `photocontest_entries`;
CREATE TABLE `photocontest_entries` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `contest` int(11) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `phone` varchar(255) DEFAULT NULL,
  `title` varchar(255) DEFAULT NULL,
  `description` text,
  `image` varchar(255) DEFAULT NULL,
  `published` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

-- ----------------------------
--  Records of `photocontest_entries`
-- ----------------------------
BEGIN;
INSERT INTO `photocontest_entries` VALUES ('1', '2', 'Daniel', 'my@email.com', '12345678', 'aar', '', 'dummy-item.jpg', '1'), ('2', '1', 'Hans', 'my@email.com', '12345678', 'Mit Billede', 'Dette er en fed beskrivelse', 'dummy-item.jpg', '1'), ('3', '2', 'Peter', 'my@email.com', '12345678', 'Andet billede', 'Dette er en fed beskrivelse', 'dummy-item.jpg', '1'), ('4', '2', 'Lucas', 'my@email.com', '12345678', 'Andet billede test', 'beskrivelse', 'dummy-item.jpg', '1'), ('5', '1', 'Ditte', 'my@email.com', '12345678', 'Billede titel', 'Anden beskrivelse', 'dummy-item.jpg', '1');
COMMIT;

SET FOREIGN_KEY_CHECKS = 1;
$sql = "SELECT 
            photocontest_winners.id, 
            photocontest_winners.contest, 
            photocontest_winners.image, 
            photocontest_winners.place, 

            photocontest_entries.id as `entry_id`, 
            photocontest_entries.contest, 
            photocontest_entries.name, 
            photocontest_entries.title,
            photocontest_entries.image

        FROM photocontest_winners 

        LEFT JOIN photocontest_entries ON photocontest_winners.contest = photocontest_entries.contest

        WHERE photocontest_winners.contest = $id AND photocontest_winners.published = 1 
        ORDER BY photocontest_winners.place";
Winners表及其示例数据:

SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
--  Table structure for `photocontest_entries`
-- ----------------------------
DROP TABLE IF EXISTS `photocontest_entries`;
CREATE TABLE `photocontest_entries` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `contest` int(11) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `phone` varchar(255) DEFAULT NULL,
  `title` varchar(255) DEFAULT NULL,
  `description` text,
  `image` varchar(255) DEFAULT NULL,
  `published` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

-- ----------------------------
--  Records of `photocontest_entries`
-- ----------------------------
BEGIN;
INSERT INTO `photocontest_entries` VALUES ('1', '2', 'Daniel', 'my@email.com', '12345678', 'aar', '', 'dummy-item.jpg', '1'), ('2', '1', 'Hans', 'my@email.com', '12345678', 'Mit Billede', 'Dette er en fed beskrivelse', 'dummy-item.jpg', '1'), ('3', '2', 'Peter', 'my@email.com', '12345678', 'Andet billede', 'Dette er en fed beskrivelse', 'dummy-item.jpg', '1'), ('4', '2', 'Lucas', 'my@email.com', '12345678', 'Andet billede test', 'beskrivelse', 'dummy-item.jpg', '1'), ('5', '1', 'Ditte', 'my@email.com', '12345678', 'Billede titel', 'Anden beskrivelse', 'dummy-item.jpg', '1');
COMMIT;

SET FOREIGN_KEY_CHECKS = 1;
$sql = "SELECT 
            photocontest_winners.id, 
            photocontest_winners.contest, 
            photocontest_winners.image, 
            photocontest_winners.place, 

            photocontest_entries.id as `entry_id`, 
            photocontest_entries.contest, 
            photocontest_entries.name, 
            photocontest_entries.title,
            photocontest_entries.image

        FROM photocontest_winners 

        LEFT JOIN photocontest_entries ON photocontest_winners.contest = photocontest_entries.contest

        WHERE photocontest_winners.contest = $id AND photocontest_winners.published = 1 
        ORDER BY photocontest_winners.place";

我认为如果您还告诉我们每个表的列的名称以及它们的1-2个条目行,那么会更全面。在这之后,我可以对可能出现的问题有一个想法

我的第一个猜测是,你正在使用的左键连接

在PhotoContatest_获奖者上加入PhotoContatest_参赛作品。竞赛=PhotoContatest_参赛作品。竞赛


这不是唯一的。在其中一个表中,您的密钥为PhotoContent_winners.contest与其他表有多个匹配项记录PhotoContent_条目。conteststrong文本。或者反过来…

如前所述,您的查询将生成每个获胜者以及特定内容的任何匹配竞赛条目。它还将返回该竞赛id的获胜者,而无竞赛条目

正如其他人所说,因为您没有使用列别名。。。如果返回的记录绑定到一个条目,则可以在数组中看到该id值

尝试这样编写查询

LEFT JOIN photocontest_entries ON
    photocontest_winners.contest = photocontest_entries.contest AND
    photocontest_winners.image = photocontest_entries.id

@Vex提供了答案-我必须将图像id添加到连接中


这就解决了我的问题。

看起来像是一对多关系,对于每个photocontest\u获奖者,photocontest\u条目中有许多条目,左连接返回所有匹配的记录。最好是提供一些样本数据和样本数据之外的预期结果。这与您所问的问题无关。。。我建议重命名查询中的字段。如果必须使用相同名称的字段,则只能获得一个值。因此,您可以执行以下操作:选择中的photocontest_winners.id作为winner_id重命名它,而不是用第二个id实例覆盖它。您可能还需要加入图像字段,但我们确实需要查看一些示例数据,我已经用sql dump更新了我的问题,其中包含了我所有表的结构和示例数据3@Vex你一针见血:-像这样的左连接解决了我的问题:左连接PhotoContatest\u条目在PhotoContatest\u获奖者上。竞赛=PhotoContatest\u条目。竞赛和photocontest_winners.image=photocontest_entries.idI已使用包含示例数据的3个表中的sql转储更新了我的问题:-在条目_id上使用列别名没有任何区别:-
LEFT JOIN photocontest_entries ON
    photocontest_winners.contest = photocontest_entries.contest AND
    photocontest_winners.image = photocontest_entries.id