Denkfehler =)
Jetzt willst Du Dir alle Beiträge ausgeben lassen, die vom user :user sind:
-- vereinfacht
SELECT
`beitrag`.`id`,
`beitrag`.`user_id`,
`beitrag`.`text`,
`beitrag_pictures`.`image`,
FROM `beitrag`
LEFT JOIN `beitrag_pictures` ON `beitrag`.`id` = `beitrag_pictures`.`beitrag_id`
WHERE `beitrag`.`user_id` = :user_id LIMIT 10;
Das heißt Du bekommst in etwa so eni Ergebnis:
id | user_id | text | image
-------------------------------
1 | 12 | Hallo | Tier.png <---- per row `beitrag` kann man nur ein bild adden
Un hier ist das Problem: Du kannst nicht 2 rows in eine selecten =)
1:n Beziehung. Per 1 Beitrag kann es n Bilder geben.
Was Du wolltest ist
id | user_id | text | image
-------------------------------
1 | 12 | Hallo | Tier.png
| | | Auth.png
die beiden Bilder in eine row selecten. Geht aber nicht.
Eine Lösung könnte hier sein (wenn Du nur eine SQL Anfrage ansenden willst)
die Bilder zusammenzufügen.
id | user_id | text | images
-------------------------------
1 | 12 | Hallo | Tier.png,Auth.png
Per PHP könntest Du den String dann wieder teilen (explode(',', $row['images'])).
----------------------
Da mich das auch interessiert hat - hier mal ein Bsp Schritt für Schritt:
Test tbl erstellen:
CREATE TABLE `user` (
`id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`username` VARCHAR(18) NOT NULL
);
CREATE TABLE `beitrag` (
`id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`user_id` INT UNSIGNED NOT NULL,
`text` VARCHAR(18) NOT NULL
);
CREATE TABLE `beitrag_pictures` (
`id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`beitrag_id` INT UNSIGNED NOT NULL,
`image` VARCHAR(255) NOT NULL
);
-- not adding idx or fk here
INSERT INTO `user` SET `username` = 'cottton';
INSERT INTO `beitrag` SET `user_id` = 1, `text` = 'text 1';
INSERT INTO `beitrag` SET `user_id` = 1, `text` = 'text 2';
INSERT INTO `beitrag` SET `user_id` = 1, `text` = 'text 3';
INSERT INTO `beitrag_pictures` SET `beitrag_id` = 2, `image` = '1.png';
INSERT INTO `beitrag_pictures` SET `beitrag_id` = 2, `image` = '2.png';
INSERT INTO `beitrag_pictures` SET `beitrag_id` = 2, `image` = '3.png';
select * from user;
+----+----------+
| id | username |
+----+----------+
| 1 | cottton |
+----+----------+
select * from beitrag;
+----+---------+--------+
| id | user_id | text |
+----+---------+--------+
| 1 | 1 | text 1 |
| 2 | 1 | text 2 |
| 3 | 1 | text 3 |
+----+---------+--------+
select * from beitrag_pictures;
+----+------------+-------+
| id | beitrag_id | image |
+----+------------+-------+
| 1 | 2 | 1.png |
| 2 | 2 | 2.png |
| 3 | 2 | 3.png |
+----+------------+-------+
Alles anzeigen
Erstmal simples ausgeben aller Beiträge von einem user:
SELECT
`b`.`id`,
`b`.`user_id`,
`b`.`text`
FROM `beitrag` AS `b`
WHERE `b`.`user_id` = 1;
+----+---------+--------+
| id | user_id | text |
+----+---------+--------+
| 1 | 1 | text 1 |
| 2 | 1 | text 2 |
| 3 | 1 | text 3 |
+----+---------+--------+
Alles anzeigen
Jetzt wollen wir die Bilder dazu:
SELECT
`b`.`id`,
`b`.`user_id`,
`b`.`text`,
`p`.`image`
FROM `beitrag` AS `b`
LEFT JOIN `beitrag_pictures` AS `p` ON `p`.`beitrag_id` = `b`.`id`
WHERE `b`.`user_id` = 1;
+----+---------+--------+-------+
| id | user_id | text | image |
+----+---------+--------+-------+
| 2 | 1 | text 2 | 1.png | <-
| 2 | 1 | text 2 | 2.png | <-
| 2 | 1 | text 2 | 3.png | <-
| 1 | 1 | text 1 | NULL |
| 3 | 1 | text 3 | NULL |
+----+---------+--------+-------+
Alles anzeigen
Geht natürlich nicht so einfach.
Wir erstellen also einen String aus n Bildern:
GROUP_CONCAT
SELECT
`b`.`id`,
`b`.`user_id`,
`b`.`text`,
GROUP_CONCAT(`p`.`image` SEPARATOR ',') AS `image_string`
FROM `beitrag` AS `b`
LEFT JOIN `beitrag_pictures` AS `p` ON `p`.`beitrag_id` = `b`.`id`
WHERE `b`.`user_id` = 1;
+----+---------+--------+-------------------+
| id | user_id | text | image_string |
+----+---------+--------+-------------------+
| 2 | 1 | text 2 | 1.png,2.png,3.png | oO?
+----+---------+--------+-------------------+
Alles anzeigen
Aber hier fehlen Beiträge!
Wir gruppieren nach Beiträgen:
SELECT
`b`.`id`,
`b`.`user_id`,
`b`.`text`,
GROUP_CONCAT(`p`.`image` SEPARATOR ',') AS `image_string`
FROM `beitrag` AS `b`
LEFT JOIN `beitrag_pictures` AS `p` ON `p`.`beitrag_id` = `b`.`id`
WHERE `b`.`user_id` = 1
GROUP BY `b`.`id`;
+----+---------+--------+-------------------+
| id | user_id | text | image_string |
+----+---------+--------+-------------------+
| 1 | 1 | text 1 | NULL |
| 2 | 1 | text 2 | 1.png,2.png,3.png |
| 3 | 1 | text 3 | NULL |
+----+---------+--------+-------------------+
Alles anzeigen
TADA!
Jetzt noch den user dazu (obwohl ich das für unnötig halte)
SELECT
`b`.`id`,
`b`.`user_id`,
`b`.`text`,
GROUP_CONCAT(`p`.`image` SEPARATOR ',') AS `image_string`,
`u`.`username`
FROM `beitrag` AS `b`
LEFT JOIN `user` AS `u` ON `u`.`id` = `b`.`user_id`
LEFT JOIN `beitrag_pictures` AS `p` ON `p`.`beitrag_id` = `b`.`id`
WHERE `b`.`user_id` = 1
GROUP BY `b`.`id`;
+----+---------+--------+-------------------+----------+
| id | user_id | text | image_string | username |
+----+---------+--------+-------------------+----------+
| 1 | 1 | text 1 | NULL | cottton |
| 2 | 1 | text 2 | 1.png,2.png,3.png | cottton |
| 3 | 1 | text 3 | NULL | cottton |
+----+---------+--------+-------------------+----------+
Alles anzeigen