如何在一个查询中进行多项选择?

时间:2014-04-07 13:29:53

标签: mysql sql database

我有2个tbl,作者和帖子。

+---------------+
|     AUTHOR    |
+---------------+
 id | name

+-----------------+
|     POST        |
+-----------------+
|id |title|content|

我希望select * from authorselect * from posts的结果并列

+------------------------------+
|           RESULT             |
+------------------------------+
id | name | id | title | content

怎么做?如何在不使用FK的情况下为超过2列进行此操作。

OBS:我正在使用MYSQL

5 个答案:

答案 0 :(得分:2)

使用join:

SELECT columns FROM Author INNER JOIN Result ON Author.id = Result.id;

希望两个表中的id都是关系。

答案 1 :(得分:2)

我们可以看到表之间没有关系,所以一个解决方案可能是:

select * from author, posts

它会创建一个矩阵

答案 2 :(得分:1)

你可以像这样选择两个表格列

SELECT authoer.id,authoer.name,posts.id,posts.name,posts.title,post.content from author,posts

如果您在两个表之间有关系,那么您的查询就像这样

select authoer.id,authoer.name,posts.id,posts.name,posts.title,post.content from author,posts
    where authoer.id=posts.id

您还使用JOIN

答案 3 :(得分:0)

试试这个:

select a.id, a.name, p.id, p.title, p.content
FROM author a
INNER JOIN post p ON a.id = p.id; 

只要两个表中的列之间存在匹配,INNER JOIN关键字就会选择两个表中的所有行。

答案 4 :(得分:-1)

尝试这个:

SELECT A.ID,A.NAME,B.ID,B.TITLE,B.CONTENT
FROM POST A
INNER JOIN AUTHOR B
ON A.ID=B.ID