如何使用PHP按照多列值从MySQL表中搜索值

时间:2017-06-01 11:54:22

标签: php mysql

我需要在表中搜索移动,名字或姓氏,并使用PHP和MySQL返回表中的行。我的桌子在下面。

DB_USER:

id           mobile        info_id             status    
1            9937229870    12                  1   
2            1234567892    13                  1
3            2123212324    14                  1

db_user_info:

info_id      fname         email               lname    
  12         Jay           as@gmail.com        Roy   
  13         Jarin         aw@gmail.com        Raot        
  14         Stoks         aq@gmail.com        Dravid

db_skill:

skill_id     info_id       category
1            13            cat1
2            14            cat2
3            15            cat3

例如,如果用户的名字是Jay Roy,他们的电子邮件是aw@gmail.com,手机是2123212324那么它会返回:

db_user_info:

info_id      fname         email               lname    
12           Jay           as@gmail.com        Roy   
13           Jarin         aw@gmail.com        Raot

DB_USER:

id           mobile        info_id             status
3            2123212324    14                  1

我该怎么做?

4 个答案:

答案 0 :(得分:0)

使用加入

  select a.*,b.*,c.* from db_user as  a inner join db_user_info b on a.info_id =b.info_id inner join db_skill c on b.info_id = c.info_id where a.mobile = "'.$usermobile.'" and b.email = "'.$useremail.'" and CONCAT(b.fname,' ',b.lname) = "'.$username.'"

答案 1 :(得分:0)

SELECT 
    db_user.*, 
    db_user_info.*, 
    db_skill.*
FROM 
    db_user, 
    db_user_info, 
    db_skill
WHERE 
    db_user_info.info_id = db_user.info_id
AND
    db_user_info.info_id = db_skill.info_id
AND (
        concat_ws(' ', db_user_info.fname, db_user_info.lname) like '%Jay Roy%'
    OR      
        db_user_info.email = 'aw@gmail.com'
    OR
        db_user_info.mobile = '2123212324'

    )

答案 2 :(得分:0)

使用此跳可以帮助您

$flname = explode(" ",$name);
$fname=  $flname[0];
$lname = isset($flname[1]) ?  $flname[1] : $flname[0];

select * from db_user du, db_user_info dui, db_skill ds where du.info_id = dui.info_id AND du.info_id = ds.info_id AND (du.mobile like '%$mobile%' OR dui.fname like '%$fname%' OR dui.lname like '%$lname%' OR dui.email like '%$email%');

编辑: 为名称搜索添加了不同的列和值

答案 3 :(得分:0)

<?php
$sql = "(SELECT content, title, 'msg' as type FROM db_user WHERE content LIKE '%" . 
           $keyword . "%' OR title LIKE '%" . $keyword ."%') 
           UNION
           (SELECT content, title, 'topic' as type FROM db_user_info WHERE content LIKE '%" . 
           $keyword . "%' OR title LIKE '%" . $keyword ."%') 
           UNION
           (SELECT content, title, 'comment' as type FROM db_skill WHERE content LIKE '%" . 
           $keyword . "%' OR title LIKE '%" . $keyword ."%')";

mysql_query($sql);
?>

=&GT;您可以尝试使用此格式搜索数据