嵌套这两个 SQL 查询没有运气
Nesting this two SQL queries with no luck
我必须使用 SQL 从 WordPress tables 获取一些数据,但我没有运气嵌套这两个查询。提前致谢!
查询 1:此查询获取按语言(WPML 插件)筛选的 wp_post 的所有数据。 Returns 类似于 wp_posts table
SELECT *
FROM wp_posts
JOIN wp_icl_translations t ON wp_posts.ID = t.element_id
AND t.element_type = CONCAT('post_', wp_posts.post_type)
WHERE wp_posts.post_type = 'post'
AND wp_posts.post_status = 'publish'
AND ( ( t.language_code = 'en' AND wp_posts.post_type = 'post' ) )
查询 2:此查询获取 wp_post 秒 table 的标题、post_name、日期、内容和特色图片 URL。
SELECT title, post_name, date, content, CONCAT(LEFT(image, LENGTH(image) - LOCATE('.', REVERSE(image))),'-150x150.',SUBSTRING_INDEX(image, '.', -1)) AS image
FROM (
SELECT
p.post_title AS title,
p.post_status AS 'status',
p.post_date AS date,
p.post_content AS content,
p.post_name AS post_name,
(SELECT `guid`
FROM wp_posts
WHERE id = m.meta_value)
AS image
FROM wp_posts p, wp_postmeta m
WHERE p.post_type = 'post'
AND p.post_status = 'publish'
AND p.id = m.post_id
AND m.meta_key = '_thumbnail_id'
ORDER BY date DESC
LIMIT 5
)TT
终于成功了!我将工作查询留在这里以防万一有人觉得它有用!
SELECT title, post_name, date, content, CONCAT(LEFT(image, LENGTH(image) - LOCATE('.', REVERSE(image))),'-150x150.',SUBSTRING_INDEX(image, '.', -1)) AS image
FROM (
SELECT
p.post_title AS title,
p.post_status AS 'status',
p.post_date AS date,
p.post_content AS content,
p.post_name AS post_name,
(SELECT `guid`
FROM wp_posts
WHERE id = m.meta_value)
AS image
FROM (
SELECT *
FROM wp_posts
JOIN wp_icl_translations t ON wp_posts.ID = t.element_id
AND t.element_type = CONCAT('post_', wp_posts.post_type)
WHERE wp_posts.post_type = 'post'
AND wp_posts.post_status = 'publish'
AND ( ( t.language_code = 'en' AND wp_posts.post_type = 'post' ) )
) p, wp_postmeta m
WHERE p.post_type = 'post'
AND p.post_status = 'publish'
AND p.id = m.post_id
AND m.meta_key = '_thumbnail_id'
ORDER BY date DESC
LIMIT 4
)TT
一种方法是获取您感兴趣的所有 post id
值,并将这些值传递给 WHERE
子句中的过滤器。类似于:
WHERE p.ID IN (
SELECT wp_posts.ID
FROM wp_posts
JOIN wp_icl_translations t ON wp_posts.ID = t.element_id
AND t.element_type = CONCAT('post_', wp_posts.post_type)
WHERE wp_posts.post_type = 'post'
AND wp_posts.post_status = 'publish'
AND ( ( t.language_code = 'en' AND wp_posts.post_type = 'post' ) ))
ORDER BY date DESC
LIMIT 4
)
这样您就可以 return 来自外部查询的所有数据,但仅用于子查询中的 post ids
。是否符合您要查找的内容?
此外,您应该为 FROM wp_posts p, wp_postmeta m
中的联接指定 JOIN
条件,否则您将得到交叉联接。
我必须使用 SQL 从 WordPress tables 获取一些数据,但我没有运气嵌套这两个查询。提前致谢!
查询 1:此查询获取按语言(WPML 插件)筛选的 wp_post 的所有数据。 Returns 类似于 wp_posts table
SELECT *
FROM wp_posts
JOIN wp_icl_translations t ON wp_posts.ID = t.element_id
AND t.element_type = CONCAT('post_', wp_posts.post_type)
WHERE wp_posts.post_type = 'post'
AND wp_posts.post_status = 'publish'
AND ( ( t.language_code = 'en' AND wp_posts.post_type = 'post' ) )
查询 2:此查询获取 wp_post 秒 table 的标题、post_name、日期、内容和特色图片 URL。
SELECT title, post_name, date, content, CONCAT(LEFT(image, LENGTH(image) - LOCATE('.', REVERSE(image))),'-150x150.',SUBSTRING_INDEX(image, '.', -1)) AS image
FROM (
SELECT
p.post_title AS title,
p.post_status AS 'status',
p.post_date AS date,
p.post_content AS content,
p.post_name AS post_name,
(SELECT `guid`
FROM wp_posts
WHERE id = m.meta_value)
AS image
FROM wp_posts p, wp_postmeta m
WHERE p.post_type = 'post'
AND p.post_status = 'publish'
AND p.id = m.post_id
AND m.meta_key = '_thumbnail_id'
ORDER BY date DESC
LIMIT 5
)TT
终于成功了!我将工作查询留在这里以防万一有人觉得它有用!
SELECT title, post_name, date, content, CONCAT(LEFT(image, LENGTH(image) - LOCATE('.', REVERSE(image))),'-150x150.',SUBSTRING_INDEX(image, '.', -1)) AS image
FROM (
SELECT
p.post_title AS title,
p.post_status AS 'status',
p.post_date AS date,
p.post_content AS content,
p.post_name AS post_name,
(SELECT `guid`
FROM wp_posts
WHERE id = m.meta_value)
AS image
FROM (
SELECT *
FROM wp_posts
JOIN wp_icl_translations t ON wp_posts.ID = t.element_id
AND t.element_type = CONCAT('post_', wp_posts.post_type)
WHERE wp_posts.post_type = 'post'
AND wp_posts.post_status = 'publish'
AND ( ( t.language_code = 'en' AND wp_posts.post_type = 'post' ) )
) p, wp_postmeta m
WHERE p.post_type = 'post'
AND p.post_status = 'publish'
AND p.id = m.post_id
AND m.meta_key = '_thumbnail_id'
ORDER BY date DESC
LIMIT 4
)TT
一种方法是获取您感兴趣的所有 post id
值,并将这些值传递给 WHERE
子句中的过滤器。类似于:
WHERE p.ID IN (
SELECT wp_posts.ID
FROM wp_posts
JOIN wp_icl_translations t ON wp_posts.ID = t.element_id
AND t.element_type = CONCAT('post_', wp_posts.post_type)
WHERE wp_posts.post_type = 'post'
AND wp_posts.post_status = 'publish'
AND ( ( t.language_code = 'en' AND wp_posts.post_type = 'post' ) ))
ORDER BY date DESC
LIMIT 4
)
这样您就可以 return 来自外部查询的所有数据,但仅用于子查询中的 post ids
。是否符合您要查找的内容?
此外,您应该为 FROM wp_posts p, wp_postmeta m
中的联接指定 JOIN
条件,否则您将得到交叉联接。