我有一个左联接的挑战,给了我很多结果。我想在比赛中列出获胜者。在我的获奖者表中,我有id,比赛,图像,地点,已发布。我感谢任何帮助! : - )
$ id返回当前的比赛ID。
在我的示例中,获胜者表只有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>");
}
这里是一个示例输出(我不明白为什么每个图像在“地方”都有int 1,2和3,因为我没有像数据库中的那样。:
Array
(
[id] => 1
[contest] => 2
[image] => dummy-item.jpg
[place] => 1
[name] => Daniel
[title] => aar
)
Array
(
[id] => 3
[contest] => 2
[image] => dummy-item.jpg
[place] => 1
[name] => Peter
[title] => Andet billede
)
Array
(
[id] => 4
[contest] => 2
[image] => dummy-item.jpg
[place] => 1
[name] => Lucas
[title] => Andet billede test
)
Array
(
[id] => 1
[contest] => 2
[image] => dummy-item.jpg
[place] => 2
[name] => Daniel
[title] => aar
)
Array
(
[id] => 3
[contest] => 2
[image] => dummy-item.jpg
[place] => 2
[name] => Peter
[title] => Andet billede
)
Array
(
[id] => 4
[contest] => 2
[image] => dummy-item.jpg
[place] => 2
[name] => Lucas
[title] => Andet billede test
)
Array
(
[id] => 1
[contest] => 2
[image] => dummy-item.jpg
[place] => 3
[name] => Daniel
[title] => aar
)
Array
(
[id] => 3
[contest] => 2
[image] => dummy-item.jpg
[place] => 3
[name] => Peter
[title] => Andet billede
)
Array
(
[id] => 4
[contest] => 2
[image] => dummy-item.jpg
[place] => 3
[name] => Lucas
[title] => Andet billede test
)
将示例数据与表格竞争:
SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for `photocontest_contests`
-- ----------------------------
DROP TABLE IF EXISTS `photocontest_contests`;
CREATE TABLE `photocontest_contests` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT NULL,
`description_image` varchar(255) DEFAULT NULL,
`prize_image` varchar(255) DEFAULT NULL,
`intro` text,
`prizes` text,
`rules` text,
`privacy_policy` text,
`time_start` datetime DEFAULT NULL,
`time_end` datetime DEFAULT NULL,
`published` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of `photocontest_contests`
-- ----------------------------
BEGIN;
INSERT INTO `photocontest_contests` VALUES ('1', 'Contest 1', 'dummy-cat.jpg', 'dummy-prize.jpg', 'EOS 5D Mark III er et fullframe digitalt spejlreflekskamera med 22,3 Megapixel, autofokus med 61 punkter samt 6 bps kontinuerlig optagelse. Optag Full HD-film i høj kvalitet, og hav fuld manuel kontrol over alt lige fra billedhastighed til lyd.', '<div class=\"row\">\n <div class=\"prize odd\">\n <div class=\"prize-desc\"><h3>1. Præmie</h3><p>EOS 5D Mark III er et fullframe digitalt spejlreflekskamera med 22,3 Megapixel, autofokus med 61 punkter samt 6 bps kontinuerlig optagelse. Optag Full HD-film i høj kvalitet, og hav fuld manuel kontrol over alt lige fra billedhastighed til lyd.</p></div>\n <div class=\"prize-image\">\n <img src=\"images/dummy-prize.jpg\" alt=\"\" />\n </div>\n <div class=\"clr\"></div>\n </div>\n </div>\n <div class=\"row\">\n <div class=\"prize even\">\n <div class=\"prize-desc\"><h3>2. Præmie</h3><p>EOS 5D Mark III er et fullframe digitalt spejlreflekskamera med 22,3 Megapixel, autofokus med 61 punkter samt 6 bps kontinuerlig optagelse. Optag Full HD-film i høj kvalitet, og hav fuld manuel kontrol over alt lige fra billedhastighed til lyd.</p></div>\n <div class=\"prize-image\">\n <img src=\"images/dummy-prize.jpg\" alt=\"\" />\n </div>\n <div class=\"clr\"></div>\n </div>\n </div>\n <div class=\"row\">\n <div class=\"prize odd\">\n <div class=\"prize-desc\"><h3>3. Præmie</h3><p>EOS 5D Mark III er et fullframe digitalt spejlreflekskamera med 22,3 Megapixel, autofokus med 61 punkter samt 6 bps kontinuerlig optagelse. Optag Full HD-film i høj kvalitet, og hav fuld manuel kontrol over alt lige fra billedhastighed til lyd.</p></div>\n <div class=\"prize-image\">\n <img src=\"images/dummy-prize.jpg\" alt=\"\" />\n </div>\n <div class=\"clr\"></div>\n </div>\n </div>', 'rules', 'privacy', '2015-05-12 14:18:02', '2015-05-28 14:18:07', '1'), ('2', 'Contest 2', 'dummy-cat.jpg', 'dummy-prize.jpg', '2 EOS 5D Mark III er et fullframe digitalt spejlreflekskamera med 22,3 Megapixel, autofokus med 61 punkter samt 6 bps kontinuerlig optagelse. Optag Full HD-film i høj kvalitet, og hav fuld manuel kontrol over alt lige fra billedhastighed til lyd.', '<div class=\"row\">\n <div class=\"prize odd\">\n <div class=\"prize-desc\"><h3>1. Præmie</h3><p>EOS 5D Mark III er et fullframe digitalt spejlreflekskamera med 22,3 Megapixel, autofokus med 61 punkter samt 6 bps kontinuerlig optagelse. Optag Full HD-film i høj kvalitet, og hav fuld manuel kontrol over alt lige fra billedhastighed til lyd.</p></div>\n <div class=\"prize-image\">\n <img src=\"images/dummy-prize.jpg\" alt=\"\" />\n </div>\n <div class=\"clr\"></div>\n </div>\n </div>\n <div class=\"row\">\n <div class=\"prize even\">\n <div class=\"prize-desc\"><h3>2. Præmie</h3><p>EOS 5D Mark III er et fullframe digitalt spejlreflekskamera med 22,3 Megapixel, autofokus med 61 punkter samt 6 bps kontinuerlig optagelse. Optag Full HD-film i høj kvalitet, og hav fuld manuel kontrol over alt lige fra billedhastighed til lyd.</p></div>\n <div class=\"prize-image\">\n <img src=\"images/dummy-prize.jpg\" alt=\"\" />\n </div>\n <div class=\"clr\"></div>\n </div>\n </div>\n <div class=\"row\">\n <div class=\"prize odd\">\n <div class=\"prize-desc\"><h3>3. Præmie</h3><p>EOS 5D Mark III er et fullframe digitalt spejlreflekskamera med 22,3 Megapixel, autofokus med 61 punkter samt 6 bps kontinuerlig optagelse. Optag Full HD-film i høj kvalitet, og hav fuld manuel kontrol over alt lige fra billedhastighed til lyd.</p></div>\n <div class=\"prize-image\">\n <img src=\"images/dummy-prize.jpg\" alt=\"\" />\n </div>\n <div class=\"clr\"></div>\n </div>\n </div>', 'rules', 'privacy', '2015-05-12 14:18:02', '2015-05-12 14:18:07', '1');
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;
包含样本数据的条目表
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;
包含样本数据的获奖者表:
SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for `photocontest_winners`
-- ----------------------------
DROP TABLE IF EXISTS `photocontest_winners`;
CREATE TABLE `photocontest_winners` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`contest` int(11) NOT NULL,
`image` int(11) DEFAULT NULL,
`place` int(11) DEFAULT NULL,
`published` tinyint(4) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
-- ----------------------------
-- Records of `photocontest_winners`
-- ----------------------------
BEGIN;
INSERT INTO `photocontest_winners` VALUES ('1', '2', '1', '1', '1'), ('4', '2', '3', '2', '1'), ('5', '2', '4', '3', '1');
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;
答案 0 :(得分:0)
我认为如果您还告诉我们每个表的列名称和1-2个入口行,它会更全面。在此之后,我可以了解可能出现的问题。
我的第一个猜测是你用来加入的键
加入photocontest_entries ON photocontest_winners.contest = photocontest_entries.contest
它不是唯一的。在其中一个表格中,您的密钥 photocontest_winners.contest 与其他表格记录有几个匹配项photocontest_entries.contest 强文本。或者另一种方式......
答案 1 :(得分:0)
如上所述,您的查询将生成每个获胜者以及针对特定竞争对手的任何匹配竞赛条目。它还将在没有参赛作品的情况下返回该比赛ID的获胜者。
正如其他人所说,因为你没有使用列别名......如果返回的记录与一个条目绑定,你会在数组中看到该id值。
尝试像这样编写查询
$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";
答案 2 :(得分:0)
@Vex提供了答案 - 我必须在连接中添加图片ID。
LEFT JOIN photocontest_entries ON photocontest_winners.contest = photocontest_entries.contest AND photocontest_winners.image = photocontest_entries.id
解决了我的问题: - )
现在我是一个快乐的露营者...感谢所有人的帮助 - 非常感谢!!!