简体   繁体   English

Mysql结果来自INNER JOIN中的SELECT IF

[英]Mysql result from SELECT IF in INNER JOIN

I want to get data from two tables. 我想从两个表中获取数据。 from the first table I need all names which are not the name of the logged-in user but the name of the logged-in user must be in one of those two columns. 从第一个表中,我需要所有名称,这些名称不是已登录用户的名称,但是已登录用户的名称必须在这两列之一中。 I want to use that result to get the name of photos from the second table for each name given from the first table. 我想使用该结果从第二张表中获取第一张表中给出的每个名称的照片名称。

 table 1 names
+++++++++++++++++++++++++
id | name1   | name2    |
+++++++++++++++++++++++++
1  | john    | lea      |<- i need lea because john is in one of those two columns
-------------------------
2  | peter   | john     |<- i need peter because john is in one of those two columns
-------------------------
3  | mark    | paola    |<- no john here so query should ignore
__________________________

table 2 users
+++++++++++++++++++++++++
id | name    | photo    |
+++++++++++++++++++++++++
1  | lea     | la.jpg   |<- I want to use lea given with SELECT IF to get name of photo
-------------------------
2  | peter   | pt.jpg   |<- I want to use peter given with SELECT IF to get name of photo
-------------------------
2  | mark    | mk.jpg   |<- no match from SELECT IF so query should ignore
-------------------------

My SELECT IF or CONCAT works fine but when i try to use it with INNER JOIN there is no results at all. 我的SELECT IF或CONCAT工作正常,但是当我尝试将其与INNER JOIN一起使用时,根本没有结果。 My codes: 我的代码:

$username = 'john';
$sql = "SELECT IF( name1 = '$username', name2, name1 ) AS otheruser
FROM names
WHERE name1 = '$username' OR name2 = '$username'";

This code above works fine. 上面的代码可以正常工作。 Now i am trying to add another table in query with INNER JOIN. 现在,我正在尝试使用INNER JOIN添加另一个查询表。 It is obvious that INNER JOIN part of code does not get otheruser result so output is "no results" . 显然,代码的INNER JOIN部分不会获得其他用户结果,因此输出为“ no results”

My attempt looks like this: 我的尝试如下所示:

$sql = "SELECT IF(names.name1 = '$username', names.name2, names.name1) AS otheruser, users.photo
FROM names
INNER JOIN users ON users.name = 'otheruser'
WHERE names.name1 = '$username' OR names.name2 = '$username'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {
$names = $row['otheruser'];
$photos = $row['photo'];
}
} else {echo "no results";}

I often like the COALESCE function in these cases, but either works. 在这些情况下,我经常喜欢使用COALESCE函数,但是两者都可行。

Try these commands and check this fiddle . 尝试这些命令并检查此小提琴

SELECT IF (name1 = 'john', name2, name1) FROM names WHERE name1 = 'john' or name2='john';

SELECT COALESCE (IF (name1 = 'john', null, name1), IF (name2 = 'john', null, name2)) FROM names WHERE 'john' IN (name1, name2);

SELECT u.username as otheruser, u.photo FROM users u
INNER JOIN names n 
    ON u.username = COALESCE(IF (name1 = 'john', null, name1), IF (name2 = 'john', null, name2))
WHERE 'john' IN (n.name1, n.name2);

You can't use the alias name in where condition (the SQL parser evaluates the select after the where condition so it does not know the select alias you are using) you should repeat the code 您不能在where条件中使用别名(SQL解析器在where条件之后评估select where condition因此它不知道您使用的选择别名),您应该重复代码

"SELECT IF(names.name1 = '$username', names.name2, names.name1) AS otheruser, users.photo
  FROM names
  INNER JOIN users ON users.name = IF(names.name1 = '$username', names.name2, names.name1)
  WHERE names.name1 = '$username' OR names.name2 = '$username'";

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

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