我有一个小问题。我的数据库中有2个表:servico
,具有ID,Nome,Profissao(管道工,电工等),数据和本地信息。canalizador
,具有ID,Nome,Profissao(仅管道工),数据和本地。
我有以下代码,以便可以进入“ canalizador”表:
mysql_connect($host,$user,$passwd) or die (mysql_error());
mysql_select_db($database);
$query = "SELECT * FROM canalizador WHERE profissao LIKE '$profissao'";
$result = mysql_query($query) or die(mysql_error());
$casaArray = array();
但是我需要一个代码,通过按菜单中每个职业(profissao)的按钮,我就可以搜索所有表格,例如“ canalizador”表格。如果我这样做:
mysql_connect($host,$user,$passwd) or die (mysql_error());
mysql_select_db($database);
$query = "SELECT * FROM canalizador, carpinteiro WHERE profissao LIKE '$profissao'";
$result = mysql_query($query) or die(mysql_error());
$casaArray = array();
我有这个错误:
where子句中的“ profissao”列不明确
最佳答案
对两个表执行UNION ALL
,在结果中使用LIKE
(作为派生表):
select id, nome, profissao, data, local
from
(
select id, nome, profissao, data, local from servico
UNION ALL
select id, nome, profissao, data, local from canalizador
) dt
where dt.profissao LIKE '$profissao'
另外,正如AsConfused建议的那样,只需
UNION ALL
:select id, nome, profissao, data, local from servico
where profissao LIKE '$profissao'
UNION ALL
select id, nome, profissao, data, local from canalizador
where profissao LIKE '$profissao'