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

时间:2022-11-29 15:43:37

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。 users表包含用户名,密码等,而我的user_info表包含用户的基本信息,如lastname,firstname等。我想知道如何在不使用两个select语句的情况下显示来自两个表的一行。

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 

2 个解决方案

#1


4  

What you want is an 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

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

#2


1  

Try this:

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

#1


4  

What you want is an 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

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

#2


1  

Try this:

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