mysql - 名字和姓氏的首字母仅在数据库查询中
问题描述
嗨,你能帮我解决我的问题吗?
我想创建一个配置文件名称,它只像这样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
解决方案
您可以使用左边的第一个字符作为姓氏
但是您不应该在 where 条件中使用左连接表列作为内部连接
将这些条件添加到 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 ";
推荐阅读
- go - 如何在 Go 中计时,耗时不到一纳秒?
- php - PHPMailer 不使用 GoDaddy 发送电子邮件
- python - 在第三个任务中使用函数值
- powershell - AzureDevOps 任务中的 Powershell Start-Process 上的访问被拒绝
- azure-active-directory - login.microsoftonline.com 不会重定向到指定的重定向 URL
- ocaml - Syntax error when Calling function inside function in Ocaml
- unity3d - 如何在 Blender 中从 Unity 重新创建模型的姿势?
- python - 如何多次获取Python中两个标记之间的子字符串?
- python - 如何解决“-:'str'和'str'不支持的操作数类型”错误
- npm - 当我尝试使用“npx 包裹文件名”进行预览时 > 它不起作用