Reputation: 3113
How to select the row entry from an users
table, if the other table has an NULL
value?
SELECT
CONCAT(`fh_users`.`username`, '_', `fh_ftp_user`.`username`) AS `userid`,
`fh_ftp_user`.`password` AS `passwd`,
'1010' AS `uid`,
'1010' AS `gid`,
`fh_ftp_user`.`path` AS `homedir`,
'/sbin/nologin' AS `shell`
FROM
`fh_ftp_user`,
`fh_users`
WHERE
`fh_ftp_user`.`username`='%U'
OR
(`fh_ftp_user`.`user_id`=`fh_users`.`id` AND `fh_users`.`username`='%U' AND `fh_ftp_user`.`username` IS NULL)"
Implicit, the table fh_ftp_user
will be selected. fh_users
will only used to CONCAT
the username with the main username.
User Table
FTP Users
Result
Expected result
If fh_ftp_user.username
is NULL
, set fh_users.username
as userid
otherwise, if fh_ftp_user.username
is not NULL
, concat both usernames with an underscore as userid
.
How i can create here an sub-query to handle these behavior?
Upvotes: 1
Views: 536