我正在研究 MySQL,我有点卡在某一点上。我想触发搜索查询以从数据库表中检索匹配记录。一切正常,但是当我要搜索使用内部选择检索到的记录时,它将不会被搜索。我不知道如何为此触发查询。
这是我的sqlfiddle演示
SQL 代码:
CREATE TABLE supplier
(
supplier_id int auto_increment primary key,
supplier_name varchar(20)
);
INSERT INTO supplier
(supplier_name)
VALUES
('billgate'),
('jobs'),
('zukkerburg');
CREATE TABLE products
(
product_id int auto_increment primary key,
supplier_id int,
product_name varchar(20)
);
INSERT INTO products
(supplier_id,product_name)
VALUES
('1','microsoft'),
('2','apple'),
('3','facebook');
查询检索所有记录:
SELECT *,(SELECT supplier_name FROM supplier WHERE supplier_id = products.supplier_id) AS supplier_name FROM products ORDER BY product_id DESC
在上面的演示中,如果我触发如下查询,它将给出正确的结果。
SELECT *,(SELECT supplier_name from supplier where supplier_id = products.supplier_id) as supplier_name FROM products WHERE product_name LIKE '%facebook%' ORDER BY product_id desc
但我想从列表中搜索供应商名称,那么我该如何实现呢?
-------------HERE YOUR SQL QUERY (ANSWER)----------
提前感谢您的帮助,非常感谢