简体   繁体   English

MySQL获取相关用户

[英]MySQL to fetch related users

I have two tables Schools and Users 我有两个表SchoolsUsers

Table School 表学校

-----------------------------------------------------
| id |     name    |  city  |     major    | userID |
----------------------------------------------------|
| 1  |  school A   | chicago |    CS       |    1   |
----------------------------------------------------|
| 2  |  school B   | chicago |    CS       |    1   |
----------------------------------------------------|
| 3  |  school A   | chicago |    CS       |    2   |
----------------------------------------------------|
| 4  |  school C   | chicago |    Art      |    2   |
----------------------------------------------------|
| 5  |  school B   | chicago |    CS       |    3   |
----------------------------------------------------|
| 6  |  school D   | chicago |    Math     |    3   |
----------------------------------------------------|
| 7  |  school A   |New York |    CS       |    3   |
----------------------------------------------------|
| 8  |  school B   | chicago |    Art      |    3   |
-----------------------------------------------------

Table Users 表用户

--------------------
| id |     name    |
____________________
| 1  |    User A   |
____________________
| 2  |    User B   |
____________________
| 3  |    User C   |
____________________
| 4  |    User D   |
____________________
| 5  |    User E   |
____________________

The userID field in the schools table is a foreign key to the id field in the users table. schools表中的userID字段是users表中id字段的外键。 I want to write a MySQL statement that takes a given userID and lists all classmates of that user. 我想编写一个MySQL语句,该语句采用给定的userID并列出该用户的所有同学。

So, for the example above, a class mate of User A (ID#1) is a user that went to the same school as User A , is located in the same city , and has the same major . 因此,对于上面的示例, User A的同学(ID#1)是与User A同一所学校,位于同一city ,并且具有相同major Thus, valid classmates for User A are only User B (ID#2) AND User C (ID#3). 因此, User A有效同学只有User B (ID#2)和User C (ID#3)。

Right now, I am using two MySQL statements to accomplish this goal. 现在,我正在使用两个MySQL语句来实现此目标。 The first one is this 第一个是这个

SELECT id FROM schools WHERE userID = '1'

which lists all schools for User A . 其中列出了User A所有学校。 Then I use PHP to loop through the results and for each row I run the following 然后,我使用PHP遍历结果,并针对每一行运行以下命令

    SELECT userID from schools WHERE 
name city LIKE '%$chicago%' 
AND name LIKE '%$school A%' 
AND major LIKE '%$CS%'

This works fine and returns the right list of userIDs. 这可以正常工作,并返回正确的用户ID列表。 However, I am wondering if there is a more efficient way to do this in one SQL statement and without having to use PHP. 但是,我想知道是否有一种更有效的方法可以在一个SQL语句中执行此操作,而不必使用PHP。

You can do it like this: 您可以这样做:

select distinct u.name
from Users u
join School s on s.userID = u.id
join (
            select distinct s.name, s.city
            from School s
            inner join Users u on u.id = s.userID
            where u.name = 'User A'
           ) aux on s.name = aux.name and s.city = aux.city
where u.name <> 'User A'

In the aux query you select the name 's and city 's of a @user and then select all users with those requirements and that aren't the @user itself. aux查询中,选择@usernamecity ,然后选择所有具有这些要求的users ,而不是@user本身。

You may replace 'User A' by a variable. 您可以将“用户A”替换为变量。

Your SCHOOL table is not considered right. 您的SCHOOL表不正确。 It violates the rules of relational database management systems. 它违反了关系数据库管理系统的规则。

One must make 3 more tables: one for school name, another for city name, and a third for major name. 一个必须再增加3个表:一个用于学校名称,另一个用于城市名称,第三个用于主要名称。 All of these must be different tables with ids. 所有这些都必须是具有ID的不同表。

Clearly name column and city column have a many-to-many relationship so one must make a lookup table for this. 显然,名称列和城市列具有多对多关系,因此必须为此创建一个查找表。

The same applies for major column. 主列也是如此。 It seems like a lot of tables but then your extraction of any value will be simple. 似乎有很多表,但是提取任何值将很简单。

OK, I made some changes to your tables. 好的,我对您的表格做了一些更改。 I prefer this way to store the data: 我更喜欢这种方式来存储数据:

table student
col.
id 
name
major
reg.no.   (to make each student unique)

table school
col.
id 
name

table city
col.
id 
name

table student_school (lookup table)
col.
student_id
school_id

table school_city
col.
school_id
city_id

Prefer these tables and you will find that every query can be made with ease. 选择这些表,您将发现可以轻松进行每个查询。 If you have any problem with understanding this, let me know. 如果您对此有任何疑问,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM