Mysql 在多个表上选择

Mysql 在多个表上选择,mysql,sql,select,Mysql,Sql,Select,我有两个表,其中包含完全独立的信息: table: tires columns: Tire_id name model etc etc table: product columns: product_id name model etc etc 我想同时在两个表上运行搜索,在名称和/或模型中查找关键字 它应该从两个数据库返回产品/轮胎,ID不是唯一的,可能存在于两个表中,是独立的产品。因此,在站点的其他部分,我使用了一个前导的T或P来保持它们在站点的编码中分开 我正在努力让搜索同时在这

我有两个表,其中包含完全独立的信息:

table: tires
columns:
Tire_id
name
model
etc 
etc

table: product
columns: 
product_id
name 
model 
etc
etc
我想同时在两个表上运行搜索,在名称和/或模型中查找关键字

它应该从两个数据库返回产品/轮胎,ID不是唯一的,可能存在于两个表中,是独立的产品。因此,在站点的其他部分,我使用了一个前导的T或P来保持它们在站点的编码中分开

我正在努力让搜索同时在这两个方面工作

我的想法是:

SELECT * FROM product OR tire WHERE name = 'keyword' OR model = 'keyword'
你需要一个工会:

select 'product' as type, product_id as id, name, model
from product
where ...
union all
select 'tire' as type, tire_id as id, name, model
from tire
where ...
提尔,这家伙

SELECT p.*,t.fieldname FROM product as p,tire as t WHERE t.name = 'keyword' OR t.model = 'keyword' or p.name = 'keyword' OR p.model = 'keyword'