连接名称并再次使用concat作为用户名?

时间:2012-01-09 20:39:37

标签: php mysql concat

我正在尝试使用PHP从数据库中提取信息并进行以下查询:

$q = "SELECT CONCAT_WS(' ',FirstName,MiddleName,LastName) AS Name,
      (NewCustomerID) AS customerid,
      substring('Firstname',1,1, 'MiddleName'),
      left(MiddleName,1) AS MN,
      (LastName) AS LN
       FROM customer 
       Where FN.MN.LN = 'username'"; 
$r = mysqli_query ($dbc, $q);
$num = mysqli_num_rows($r);

当我运行这个时,我收到信息但不是用户名,我需要:

$q = "SELECT CONCAT_WS(' ',FirstName,MiddleName,LastName) AS Name,
      (NewCustomerID) AS customerid,
      left('Firstname',1) as FN,
      left(MiddleName,1) AS MN,
      (LastName) AS LN 
            FROM customer".

刚刚卡住了,尝试过很多东西,却无法让它发挥作用。

3 个答案:

答案 0 :(得分:1)

由于FNMNLN是别名,因此您需要使用HAVING代替WHERE

HAVING FN.MN.LN = 'username'

虽然FN.MN.LN是语法错误,但我认为您的意思是CONCAT(FN,MN,LN)

HAVING CONCAT(FN,MN,LN) = 'username'

答案 1 :(得分:0)

您没有正确引用查询,名字是列名:

SELECT 
    CONCAT_WS(' ',FirstName,MiddleName,LastName) AS Name,
    (NewCustomerID) AS customerid,
    LEFT(Firstname,1) as FN, 
    LEFT(MiddleName,1) AS MN,
    (LastName) AS LN
FROM customer
WHERE CONCAT(FN,MN,LN) = 'username'

答案 2 :(得分:0)

$q = "SELECT CONCAT(left(FirstName,1),left(MiddleName,1),LastName) AS UserName, CONCAT_WS(' ',LastName,FirstName) AS Name, (NewCustomerID) AS customerid FROM customer ";