doufan9395 2012-06-04 17:20
浏览 31
已采纳

如何选择两行串联的项目就像输入一样

i have mysql table, users: (id, first_name, last_name, ....)

i'd like to do something like this pseudo query

SELECT * FROM users WHERE first_name.' '.last_name = 'john doe' LIMIT 10")

I Want to do this cause i have lots of trouble spliting the string (then i don't know in what order is user typing'

This is my current, not working very good

$phrase = explode(' ',$term);
$last_name = '';
if($phrase[1] != '')
    $last_name= " OR last_name LIKE '%".$phrase[1]."%'";
$qstring = "SELECT usuarios.first_name,usuarios.last_name,
                    usuarios.id as id
            FROM usuarios
            WHERE first_name LIKE '%".$phrase[0]."%' OR last_name LIKE '%".$phrase[0]."%' $last_name LIMIT 5";

Any suggestion to achieve this (by concatenating at query or spliting at php) would be very apreciated

  • 写回答

2条回答 默认 最新

  • dps43378 2012-06-04 17:21
    关注

    You can concatenate in a MySQL query using CONCAT:

    SELECT * FROM users WHERE CONCAT(first_name,' ',last_name) = 'john doe' LIMIT 10
    

    In your code this would become:

    SELECT * FROM usuarios WHERE CONCAT(first_name,' ',last_name) LIKE '%{$phrase[0]}%' LIMIT 5
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法