如何從連接中的選擇字段使用自定義別名字段?如何使用連接中選擇字段的自定義別名字段?
SELECT LOWER(CONCAT(`ufn`.`value`, '.', `uln`.`value`, CAST(RAND()*1e6 AS UNSIGNED))) AS `newlogin`
FROM `user_firstname` AS `ufn`
INNER JOIN `user_lastname` AS `uln`
LEFT JOIN `user` AS `u` ON `u`.`login` = `newlogin`
WHERE `ufn`.`sex` = 'male' AND `u`.`id` IS NULL
ORDER BY RAND()
LIMIT 1
編輯:
我的解決方案,但...:
SELECT @newlogin
FROM (
SELECT @newlogin := LOWER(CONCAT(`ufn`.`value`, '.', `uln`.`value`, CAST(RAND()*1e6 AS UNSIGNED)))
FROM `user_firstname` AS `ufn`
INNER JOIN `user_lastname` AS `uln`
WHERE `ufn`.`sex` = 'male'
ORDER BY RAND()
LIMIT 1
) AS `ufnX`
LEFT JOIN `user` AS `u` ON `u`.`login` = @newlogin
WHERE `u`.`id` IS NULL
範圍問題。將第一次加入派生表中! – jarlh