试图加入两个MySQL查询

时间:2015-01-27 09:59:13

标签: php mysql database

我想在单个查询中使用这两个不同的表结果。

$result=mysqli_query("SELECT * FROM table_1 WHERE keyskill LIKE'php%'");
$result=mysqli_query("SELECT * FROM table_2 WHERE parentaddress LIKE'chennai%'");

任何人都可以帮助我吗?

这是表结构......

表1:

  ID | F. Name | L.Name | Gender | Parentaddress  
___________________________________________________
 12  | Peter   | Son    |  Male  |  Chennai
 13  | Johny   | Depp   |  Male  |  Coimbatore

表2:

 S.No|    Name | Title   | Gender | Keyskill  
 ___________________________________________________
 13  | Johny   | Student |  Male  |  PHP
 12  | Peter   | Student |  Male  |  JAVA

2 个答案:

答案 0 :(得分:0)

你的数据库不应该是这样的吗? (只是猜测!)

表1(人员):

  ID | F. Name | L.Name | Gender | Parentaddress  | Title  
__________________________________________________________
 12  | Peter   | Son    |  Male  |  Chennai       | Student
 13  | Johny   | Depp   |  Male  |  Coimbatore    | Student

表2(技能):

 S.No| ID | Keyskill  
 ___________________________________________________
 70  | 13 | PHP
 71  | 13 | Whatever Language
 72  | 12 | JAVA

表1定义了您想要保存技能的人员,表2仅引用了iD字段的人员。

答案 1 :(得分:0)

使用UNION:

select a.`ID` as ID, a.`F.Name` as first_name, a.`L.Name` as last_name, b.`Title` as tittle, a.`Gender` as gender, a.`Parentaddress` as parent_address, b.keyskill as keyskill from table_1 a inner join table_2 b on a.id = b.`S.No` where b.keyskill LIKE 'php%'
union all
select a.`S.No` as ID, b.`F.Name` as first_name, b.`L.Name` as last_name, a.`Title` as tittle, a.`Gender` as gender, b.`Parentaddress` as parent_address, a.`Keyskill` as keyskill from table_2 a inner join table_1 b where b.parentaddress LIKE 'chennai%'

或者可能是ID加入:

select * from table_1 a left outer join table_2 b on a.id = b.`S.No` where b.keyskill LIKE 'php%' and a.parentaddress LIKE 'chennai%'