我有这个表page_base
,我想在另一个名为position = 1
page_base_images
的表格中加入ON page_base.id = page_base_images.page_base_id
中的一个图片。
page_base [table]
+----+---------+------------+
| id | menu_id | submenu_id |
+----+---------+------------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
+----+---------+------------+
page_base_images [table]
+----+--------------+------------+----------+--------+
| id | page_base_id | image | position | active |
+----+--------------+------------+----------+--------+
| 1 | 1 | 01_001.jpg | 1 | 1 |
| 2 | 1 | 01_002.jpg | 2 | 1 |
| 3 | 1 | 01_003.jpg | 3 | 1 |
| 4 | 1 | 01_004.jpg | 4 | 1 |
| 5 | 1 | 01_005.jpg | 5 | 1 |
| 6 | 1 | 01_006.jpg | 6 | 1 |
| 7 | 1 | 01_007.jpg | 7 | 1 |
| 8 | 1 | 01_008.jpg | 8 | 1 |
| 9 | 1 | 01_009.jpg | 9 | 1 |
| 10 | 1 | 01_010.jpg | 10 | 1 |
| 11 | 3 | 03_001.jpg | 1 | 1 |
| 12 | 3 | 03_002.jpg | 2 | 1 |
| 13 | 3 | 03_003.jpg | 3 | 1 |
+----+--------------+------------+----------+--------+
所需的数组结果
+----------+--------------+----------+----------+-------------+------------+
| array_id | page_base_id | title | subtitle | description | image |
+----------+--------------+----------+----------+-------------+------------+
| 0 | 1 | *string* | *string* | *string* | 01_001.jpg |
| 1 | 3 | *string* | *string* | *string* | 03_001.jpg |
+----------+--------------+----------+----------+-------------+------------+
到目前为止,我有这个,但它始终使用id = 1
添加图像,如果我放了一个WHERE
子句,它会给我错误。
$page_base_table = get_raw_query("
SELECT
base_lang.title, base_lang.subtitle, base_lang.description, base_image.image
FROM page_base base
LEFT OUTER JOIN page_base_languages base_lang ON base.id = base_lang.page_base_id
LEFT OUTER JOIN
(SELECT image.page_base_id, image.image FROM page_base_images image LIMIT 1)
base_image ON base.id = base_image.page_base_id
WHERE base_lang.title LIKE '%$string%' ");
有人可以帮我限制page_base_images加入为1并使用position = 1
抓取图片吗?
答案 0 :(得分:1)
您可以通过在page_base_images
的加入中添加其他条件来重写您的查询,如ON (base.id = base_image.page_base_id AND base_image.position =1)
SELECT
base_lang.title,
base_lang.subtitle,
base_lang.description,
base_image.image
FROM
page_base base
LEFT OUTER JOIN page_base_languages base_lang
ON base.id = base_lang.page_base_id
LEFT OUTER JOIN page_base_images AS base_image
ON (base.id = base_image.page_base_id AND base_image.position =1)
WHERE base_lang.title LIKE '%$string%'
编辑以获取具有最低位置的图片
SELECT
base_lang.title,
base_lang.subtitle,
base_lang.description,
base_image.image
FROM
page_base base
LEFT OUTER JOIN page_base_languages base_lang
ON base.id = base_lang.page_base_id
LEFT OUTER JOIN
(SELECT page_base_id,image,MIN(`position`) FROM page_base_images GROUP BY page_base_id)
AS base_image
ON (base.id = base_image.page_base_id)
WHERE base_lang.title LIKE '%$string%
答案 1 :(得分:0)
删除子查询并尝试:
SELECT
base_lang.title,
base_lang.subtitle,
base_lang.description,
base_image.image
FROM page_base base
LEFT OUTER JOIN page_base_languages base_lang
ON base.id = base_lang.page_base_id
LEFT OUTER JOIN page_base_images
ON base.id = page_base_images.page_base_id
AND page_base_images.position = 1
WHERE base_lang.title LIKE '%$string%'
答案 2 :(得分:0)
根据我的理解,你的page_base_images表中将不会有两个position = 1的图像。你尝试过这样的事情:
$page_base_table = get_raw_query("SELECT base_lang.title, base_lang.subtitle, base_lang.description, base_image.image FROM page_base base LEFT OUTER JOIN page_base_languages base_lang ON base.id = base_lang.page_base_id LEFT OUTER JOIN page_base_images image AS base_image ON base.id = base_image.page_base_id AND base_image.position=1 WHERE base_lang.title LIKE '%$string%' ");
答案 3 :(得分:0)
你也可以从page_base_images开始你的选择然后离开加入语言并将你的结果留在你的结果上,在page_base_images.page_id列上有一个不同的。
这将为您提供所需的内容,并且听起来比其他解决方案更具逻辑性和错误安全性。
假设image_position始终是唯一的,并且我认为image_position 1存在不够安全。
SELECT
base_lang.title,
base_lang.subtitle,
base_lang.description,
base_image.image,
DISTINCT(base.id) as baseId
FROM
page_base_images base_image
LEFT OUTER JOIN page_base base
ON (base.id = base_image.page_base_id)
LEFT OUTER JOIN page_base_languages base_lang
ON base.id = base_lang.page_base_id
WHERE base_lang.title LIKE '%$string%'