仅在数据库查询时的名字和姓氏首字母

时间:2019-01-30 21:06:27

标签: mysql sql

嗨,您能帮我解决我的问题吗?

我想创建一个配置文件名称,其名称只能像这样Sergio E.而不是这样Sergio Encabo

这是我的查询

SELECT p.id, p.title, COUNT(b.id) AS bids, p.slug, p.`description`, p.budget AS budget, p.`isFeatured`, p.`slug`, u.`profileLink`, u.`profilePhoto`, CONCAT_WS(' ', u.firstName , u.lastName ) AS fullName 
FROM tbl_projects AS p 
LEFT JOIN tbl_users AS u ON p.userId = u.userId 
LEFT JOIN tbl_bids AS b ON p.`id` = b.`projectId` 
WHERE p.`isActive` = 'y' AND u.`isActive` = 'y' AND p.`jobStatus` = 'open' AND p.userId=? 
GROUP BY p.id 
ORDER BY p.`id` DESC 

1 个答案:

答案 0 :(得分:1)

您可以使用左侧的第一个字符作为姓氏

但是在这种情况下,您不应该使用左联接表列作为内部联接
将这些条件添加到on子句

    $qry = "SELECT p.id
        , p.title
        , COUNT(b.id) AS bids
        , p.slug
        , p.`description`
        , p.budget AS budget
        , p.`isFeatured`
        , p.`slug`
        , u.`profileLink`
        , u.`profilePhoto`
        , CONCAT( u.firstName ,' ', left(u.lastName,1), '.' ) AS fullName 
        FROM tbl_projects AS p 
        LEFT JOIN tbl_users AS u ON p.userId = u.userId   AND u.`isActive` = 'y' 
        LEFT JOIN tbl_bids AS b ON p.`id` = b.`projectId`
        WHERE p.`isActive` = 'y' 
        AND p.`jobStatus` = 'open' 
         AND p.userId=? 
         GROUP BY p.id 
         ORDER BY p.`id` DESC  ";