如何选择2个字符串列的组合等于该值的行

时间:2017-01-25 17:01:45

标签: mysql sql mysqli

我有一个表名雇员,它有列FirstName和LastName

现在我想获取表中FirstName和LastName等于值的特定行中的所有数据。

示例:Value = Mark Gabriel

其中:FirstName = Mark        LastName = Gabriel

我试过这段代码。

$search = 'Mark Gabriel';

$query = mysqli_query($con,"SELECT * FROM employees WHERE CONCAT(FirstName, ' ', LastName) = '$search' ");
 While($row = mysqli_fetch_array($query)){
     echo $row['FirstName']; }


$query = mysqli_query($con, "SELECT * FROM employees WHERE FirstName LIKE '". $search."%' AND LastName LIKE '" .$search. "%' ");
  While($row = mysqli_fetch_array($query)){
     echo $row['FirstName'];
 }

我没有从我试过的查询中得到任何回复。

任何评论都表示赞赏。谢谢

1 个答案:

答案 0 :(得分:-1)

你可以尝试一下:

SELECT *, CONCAT(firstName, ' ', lastName) as search FROM employees HAVING search = '$search'