Search code examples
phpsqlsql-like

How to search with LIKE in columns merged with AS


I merged two column with AS , now I want to search data inside them with LIKE. How can i do that? Here is what I have tries so far.

Php :

$sql = "SELECT CONCAT (name , ' ' , surname) AS full  FROM person ";
$query = mysqli_query($con,$sql);
if($query){
    while($row=mysqli_fetch_assoc($query)){
        echo "<p>$row[full]</p>";
    }
}else{
    echo mysqli_error($con);
}

Solution

  • I think you could try like this - using the having clause rather than where

    select concat( field1, field2 ) as 'alias'
    from `table`
    having `alias` like '%word%'
    

    or, in your case:

    SELECT CONCAT (`name` , ' ' , `surname`) AS 'full'  FROM `person`
    having `full` like '%word%';