MySQL SQL在一个查询中选择两个表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14743070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:26:22  来源:igfitidea点击:

SQL Selecting two tables in one query

mysqlsql

提问by aaa

I have two tables which are related to each other in my mysql database, the users and the user_info. users table contains the username, password etc. while my user_info table contains basic information of users like lastname, firstname, etc. I was wondering how to display one row which will came from both tables without using two select statement.

我的 mysql 数据库中有两个相互关联的表,users 和 user_info。用户表包含用户名、密码等,而我的 user_info 表包含用户的基本信息,如姓氏、名字等。我想知道如何在不使用两个选择语句的情况下显示来自两个表的一行。

It is something like this: (I know this is not the correct format. sorry)

它是这样的:(我知道这不是正确的格式。抱歉)

SELECT * FROM users AND user_info WHERE users.user_id == user_info.user_id 

回答by Jeremy Wiggins

What you want is an inner join.

你想要的是一个inner join.

SELECT *
FROM Users
INNER JOIN User_Info on Users.User_Id = User_Info.User_Id

You can read more about select statements, joins, etc. here:
MySQL Reference - Select Syntax

您可以在此处阅读有关选择语句、连接等的更多信息:
MySQL 参考 - 选择语法

回答by nonzero

Try this:

尝试这个:

SELECT users.user_id, user_info.user_id 
FROM users, user_info 
WHERE users.user_id = user_info.user_id;