简体   繁体   中英

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. 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. It is obvious that INNER JOIN part of code does not get otheruser result so output is "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.

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

"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'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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