나는이 같은 두 개의 테이블이 있습니다null을 어떻게 대체 할 수 있습니까?
// users
+----+--------+------------+----------------+------------+
| id | name | reputation | avatar | visibility |
+----+--------+------------+----------------+------------+
| 1 | John | 344 | er0942334r4... | 1 |
| 2 | Peter | 65544 | 340ojfc343f... | 0 |
+----+--------+------------+----------------+------------+
// posts
+----+--------+------------+-----------+
| id | title | content | author_id |
+----+--------+------------+-----------+
| 1 | title1 | content1 | 2 |
| 2 | title2 | content2 | 1 |
+----+--------+------------+-----------+
또한 여기 내 쿼리입니다 :
SELECT p.*, u.name, u.reputation rep, u.avatar
FROM posts p
LEFT JOIN users u
ON u.id = p.author_id AND u.visibility = 1
// please focus on this-> ^^^^^^^^^^^^^^^^
그리고 여기 현재 결과입니다
+----+--------+------------+-----------+--------+------+----------------+
| id | title | content | author_id | name | rep | avatar |
+----+--------+------------+-----------+--------+------+----------------+
| 1 | title1 | content1 | 2 | | | |
| 2 | title2 | content2 | 1 | John | 344 | er0942334r4... |
+----+--------+------------+-----------+--------+------+----------------+
지금 내가 설정하려면 일부 기본값은 visibility <> 1
또는 name or rep or avatar is null
입니다.
+----+--------+------------+-----------+--------+------+----------------+
| id | title | content | author_id | name | rep | avatar |
+----+--------+------------+-----------+--------+------+----------------+
| 1 | title1 | content1 | 2 | NoName | 1 | default |
| 2 | title2 | content2 | 1 | John | 344 | er0942334r4... |
+----+--------+------------+-----------+--------+------+----------------+
내가 어떻게 할 수 있습니다
: 그래서 여기 예상 결과?참고 : 내가 이런 PHP로 그렇게 할 수 있습니다
$name = is_null($result['name']) ? 'NoName' : $result['name'];
$name = is_null($result['rep']) ? 1 : $result['rep'];
$name = is_null($result['avatar']) ? 'default' : $result['avatar'];
하지만 내가 할 수있는 방법을 알고 싶어요 그 순수 SQL에 의해.
편도로 케이스를 사용하십시오. – Drew