mysql - 加入或连接mysql时出现语法错误

标签 mysql sql

我想检索 mysql 结果,但我的查询不起作用,因为我是计算机编程或计算机数据库的新手。

SELECT guid, post_name
FROM `wp_posts`
WHERE `post_parent` = 0 and `post_type` = 'post' and post_title != 'Auto Draft'     

返回:

guid                    post_name   
http://localhost/?p=21  post-2-title
http://localhost/?p=12  post-4-title

还有

SELECT guid as children_url, post_parent as post_parent
FROM `wp_posts`
WHERE `post_parent` != 0 and `post_type` = 'attachment' and post_title != 'Auto Draft' 

返回:

children_url                               post_parent  
http://localhost/attachment-post-1            12
http://localhost/attachment-post-2            12
http://localhost/attachment-post-3            12
http://localhost/attachment-post-4            12
http://localhost/attachment-post-5            12
http://localhost/attachment-post-6            12
http://localhost/attachment-post-7            12
http://localhost/attachment-post-8            12
http://localhost/attachment-post-9            21
http://localhost/attachment-post-10           21
http://localhost/attachment-post-11           21
http://localhost/attachment-post-12           21
http://localhost/attachment-post-13           21
http://localhost/attachment-post-14           21
http://localhost/attachment-post-15           21
http://localhost/attachment-post-16           21

我尝试了多次查询但没有成功,

SELECT guid as children_url, post_parent as post_parent
FROM `wp_posts`
WHERE `post_parent` != 0 and `post_type` = 'attachment' and post_title != 'Auto Draft'
  and SELECT guid, post_name FROM `wp_posts`
      WHERE `post_parent` = 0 and `post_type` = 'post' and post_title != 'Auto Draft' 

引发错误:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT guid, post_name FROM wp_posts WHERE post_parent = 0 and post_type =' at line 1

我想要的结果是这样的:

 children_url                              post_parent              post_paraent_guid       post_parent_title
http://localhost/attachment-post-1            12                    http://localhost/?p=12  post-2-title
http://localhost/attachment-post-2            12                    http://localhost/?p=12  post-2-title
http://localhost/attachment-post-3            12                    http://localhost/?p=12  post-2-title
http://localhost/attachment-post-4            12                    http://localhost/?p=12  post-2-title
http://localhost/attachment-post-5            12                    http://localhost/?p=12  post-2-title
http://localhost/attachment-post-6            12                    http://localhost/?p=12  post-2-title
http://localhost/attachment-post-7            12                    http://localhost/?p=12  post-2-title
http://localhost/attachment-post-8            12                    http://localhost/?p=12  post-2-title
http://localhost/attachment-post-9            21                    http://localhost/?p=21  post-4-title
http://localhost/attachment-post-10           21                    http://localhost/?p=21  post-4-title
http://localhost/attachment-post-11           21                    http://localhost/?p=21  post-4-title
http://localhost/attachment-post-12           21                    http://localhost/?p=21  post-4-title
http://localhost/attachment-post-13           21                    http://localhost/?p=21  post-4-title
http://localhost/attachment-post-14           21                    http://localhost/?p=21  post-4-title
http://localhost/attachment-post-15           21                    http://localhost/?p=21  post-4-title
http://localhost/attachment-post-16           21                    http://localhost/?p=21  post-4-title

如有任何帮助,我们将不胜感激。

谢谢

最佳答案

试试这个

SELECT
    pc.guid as children_url,
    pc.post_parent as post_parent,
    pp.guid as post_paraent_guid,
    pp.post_title as post_parent_title
FROM
    wp_posts pc
    left join wp_posts pp on pp.ID=pc.post_parent
WHERE
    pc.post_type = 'attachment'
    and pc.post_title != 'Auto Draft'
ORDER BY
    pc.guid

关于mysql - 加入或连接mysql时出现语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39868022/

相关文章:

mysql - 使用波兰字母订购

mysql - 如何在mysql中正确存储和显示unicode

mysql - 让 MySQL 5.7 显示毫秒,而不是微秒

sql - 用排名替换子选择

sql - 多列平均值

mysql - 如何使用数组创建 MySQL 查询?

mysql - 添加表中每一行的值并输出(将 JSON 字符串转换为 int)

mysql - 加速大型数据库的 "SQL_CALC_FOUND_ROWS"SQL 查询

mysql - 查找相关结果,即使它有长值 SQL

sql - 如何在sql脚本中设置CHECK_POLICY OFF