我正在从PHP查询MS SQL Server数据库。该查询有效,但是当我尝试在first_name和last_name字段之间插入空格时,我收到错误。
当我这样写时,查询有效:
$query = 'SELECT meetings.name as Subject,
meetings.date_entered,
meetings.date_modified,
SELECT (users.first_name + users.last_name) as Fullname FROM dbo.users WHERE users.id = meetings.assigned_user_id';
if($custom_join) {
$query .= $custom_join['select'];
}
$query .= ' FROM meetings ';
然而,当我写作时它失败了我试图在名字和姓氏之间加上一个空格,如下所示:
$query = 'SELECT meetings.name as Subject,
meetings.date_entered,
meetings.date_modified,
SELECT (users.first_name + ' ' + users.last_name) as Fullname FROM dbo.users WHERE users.id = meetings.assigned_user_id';
if($custom_join) {
$query .= $custom_join['select'];
}
$query .= ' FROM meetings ';
我得到的错误是''不是SQL表中的有效列或类似的列。
我用作(全名)的全名也是小写字母: - >全名。为什么这是大写' F'变成小写' f'?
我主要研究MySQL,但我对MS SQL相当新......有点奇怪。即使是Concat功能也不起作用。当我尝试使用它时,它告诉我它不是内置函数。
修改
$query = 'SELECT meetings.name as Subject,
meetings.date_entered,
meetings.date_modified,
(SELECT (users.first_name + ' ' + users.last_name) as Fullname
FROM dbo.users WHERE users.id = meetings.assigned_user_id)';
编辑2:
$query = 'SELECT meetings.name as Subject,
meetings.date_entered,
meetings.date_modified,
SELECT (users.first_name + '' '' + users.last_name) as Fullname
FROM dbo.users WHERE users.id = meetings.assigned_user_id';
if($custom_join) {
$query .= $custom_join['select'];
}
$query .= ' FROM meetings ';
答案 0 :(得分:1)
答案 1 :(得分:1)
如果使用CONCAT功能会怎样?
SELECT CONCAT(users.first_name, SPACE(1) ,users.last_name) AS Result FROM dbo.users WHERE users.id = meetings.assigned_user_id
答案 2 :(得分:1)
从上下文来看,你似乎没有在SSMS中运行这个确切的查询,所以你可能错过了逃避你想要离开的单引号所需的额外单引号:
(SELECT users.first_name + '' '' +users.last_name as Fullname)
更新:我从你的问题更新中看到我是正确的,但你也有一个无效的查询,没有第二个FROM
条款,很难确切地说出你所追求的是什么,但也许:
$query = 'SELECT meetings.name as Subject,
meetings.date_entered,
meetings.date_modified,
users.first_name + '' '' + users.last_name as Fullname
FROM dbo.users
JOIN dbo.meetings
ON users.id = meetings.assigned_user_id)
';
答案 3 :(得分:0)
问题不在于连接,而是一个格式错误的SQL查询。串联问题是由单引号字符串中的单引号引起的。
以下代码应返回有效查询:
$query = "SELECT meetings.name as Subject,
meetings.date_entered,
meetings.date_modified,
(SELECT users.first_name + ' ' + users.last_name
FROM dbo.users WHERE users.id = meetings.assigned_user_id
) as Fullname
";
//$query = 'SELECT meetings.*';
if($custom_join) {
$query .= $custom_join['select'];
}
$query .= " FROM meetings ";
这是查询的样子:
$query = "SELECT meetings.name as Subject,
meetings.date_entered,
meetings.date_modified,
(SELECT users.first_name + ' ' + users.last_name)
FROM dbo.users WHERE users.id = meetings.assigned_user_id
) as Fullname
FROM meetings";