简体   繁体   English

两个SELECT / WHERE子句组合在一起?

[英]Two SELECT/WHERE clauses combined?

I'm not very good with MySQL statements, so I'm looking for some insight. 我对MySQL语句不太满意,所以我正在寻找一些见解。 Let's say a user visits someone else's page. 假设用户访问其他人的页面。 This will send the username as a parameter to a script to check if the current user and the user's page they are visiting are friends. 这会将用户名作为参数发送到脚本,以检查当前用户和他们访问的用户页面是否为朋友。

My simple database structure: 我简单的数据库结构:

FRIENDS TABLE -- relationship_id, friend1_ID, friend2_ID, status FRIENDS TABLE - relationship_id, friend1_ID, friend2_ID, status

ACTIVE_USERS TABLE -- id, username, email ...etc. ACTIVE_USERS TABLE - id, username, email ...等。

My current SQL statement is as follows: 我当前的SQL语句如下:

SELECT * 
FROM friends
JOIN active_users ON ( active_users.username =  '$username' ) 
WHERE (
    friends.friend_1ID = '$current_user_ID'
    AND friends.friend_2ID = active_users.id
    AND friends.status = 1
)

While this WORKS, I don't feel it's elegant. 虽然这个工作,我觉得它不优雅。 It returns a row if the two are friends, but this row obviously is a join of the user information row and the relationship row. 如果两者是朋友,则返回一行,但该行显然是用户信息行和关系行的连接。 I would just like to grab the relationship row. 我想抓住关系排。 Would it be economical to break this up into two statements? 把它分成两个陈述是否经济? For instance, I would query for the visited page's user ID using the username, and then use that variable in a second statement to grab the relationship. 例如,我将使用用户名查询被访问页面的用户ID,然后在第二个语句中使用该变量来获取关系。 Or is there an easier syntax I have not come across? 或者是否有一种我没有遇到的更简单的语法? Help is appreciated! 感谢帮助!

You can do as follows: 你可以这样做:

SELECT b.* 
FROM friends b
JOIN active_users ON ( active_users.username =  '$username' ) 
WHERE (
    b.friend_1ID = '$current_user_ID'
    AND b.friend_2ID = active_users.id
    AND b.status = 1
)

By using an alias named b for friends table, we are able to just select fields that belong to that table by using select b.* , instead of the default * which covers all your joined tables so that's why it was returning all fields from the tow joined tables. 通过为friends表使用名为b的别名,我们只需使用select b.*选择属于该表的字段,而不是覆盖所有连接表的默认* ,这就是它返回所有字段的原因。两个联合表。

So we just gave alias name to the tables in the query to be able to only select the fields belonging to that alias table. 因此,我们只为查询中的表提供了别名,以便只能选择属于该别名表的字段。

A JOIN is the preferred way to fetch data from multiple tables. JOIN是从多个表中获取数据的首选方法。 It is faster than doing two separate calls. 它比做两个单独的调用更快。

I'm not that familiar with MySQL myself, but shouldn't it be more like: 我自己并不熟悉MySQL,但不应该更像:

SELECT b.* 
FROM friends b
JOIN active_users ON ( b.friend_2ID = active_users.id AND active_users.username =  '$username' ) 
WHERE (
    b.friend_1ID = '$current_user_ID'        
    AND b.status = 1
)

(modified Nelson's code here) (修改了尼尔森的代码)
Probably the optimizer would produce the same , but somehow I find it more logical to define the relation in the JOIN. 可能优化器会生成相同的 ,但不知何故,我发现在JOIN中定义关系更合乎逻辑。

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

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