0

在 sqlite 中搜索时,我使用以下 SQL 搜索所有列一次:

Select id from table where '--' || IFNULL(name, ' ')  
|| '--' || IFNULL(created, ' ')  || '--' || IFNULL(desc, ' ')  
|| '--' LIKE '%query%'

现在我需要搜索一个order表和order details表。他们看起来像这样

订单表

id
date
customer_id

订单详细信息

id
product_id
cost
orders_id
qty

在这种情况下如何搜索,我尝试使用连接:

Select o.id,o.customer_id from orders o where '--' 
(Select * from customers where name like '%query%' and id=customer_id) '--' 
||  (Select * from product where name like '%query%' and ID =
(Select product_id from order_details where id =odetails_id)) '--' LIKE '%query%'

编辑

我尝试了这个新查询,但在“或”处出现错误

SELECT o.id FROM orders o WHERE o.id in

((SELECT order_id FROM order_items WHERE order_items.service_id In
(SELECT id FROM services WHERE name like query))  
OR o.id in
(SELECT order_id FROM order_items WHERE order_items.customer_id In
(SELECT id FROM customers WHERE name like query)) 
OR o.id in
(SELECT order_id FROM order_items WHERE order_items.cloth_id In
(SELECT id FROM cloths WHERE name like query) ))

我该如何解决

4

1 回答 1

2

您的查询包含一些错误:

  1. 几个|| 缺失
  2. 您在预期标量值的地方选择 * 两次

假设缺失的表如下

create table product (id, name);
create table customers (id, name);

您的查询可以这样修复

Select o.id,o.customer_id 
from orders o 
where '--'  || 
      ( select name 
        from customers 
        where name like '%query%' and id=customer_id ) || 
  '--'  ||  
      ( select name 
        from product 
        where name like '%query%' and 
              id = ( select product_id 
                     from order_details 
                     where id =odetails_id ) ) || 
  '--' 
  LIKE '%query%';

但是,我建议您将标量子查询替换为连接

select o.id, o.customer_id 
from orders o 
     join customers c on o.customer_id = c.id
     join order_details d on o.odetails_id = d.id
     join product p on d.product_id = p.id         
where c.name = '%query' and
      p.name = '%query' and
      '--' || c.name || '--' || p.name || '--' like '%query%';

编辑

您的第二个查询有一个无关的括号。

SELECT o.id 
FROM   orders o 
WHERE  o.id in 
       ( SELECT order_id 
         FROM   order_items 
         WHERE  order_items.service_id In
                ( SELECT id 
                  FROM   services 
                  WHERE  name like '%query%'))
   OR  o.id in 
       ( SELECT order_id 
         FROM   order_items 
         WHERE  order_items.customer_id In
                ( SELECT id 
                  FROM   customers 
                  WHERE  name like '%query%')) 
   OR o.id in
       ( SELECT order_id 
         FROM   order_items 
         WHERE  order_items.cloth_id In
                ( SELECT id 
                  FROM   cloths 
                  WHERE  name like '%query%'));

但是,为清楚起见,您可以将 OR 推入子查询:

SELECT o.id 
FROM   orders o 
WHERE  o.id in 
       ( SELECT order_id 
         FROM   order_items 
         WHERE  order_items.service_id In
                ( SELECT id 
                  FROM   services 
                  WHERE  name like '%query%')
            OR   order_items.customer_id In
                 ( SELECT id 
                   FROM   customers 
                   WHERE  name like '%query%') 
            OR   order_items.cloth_id In
                 ( SELECT id 
                   FROM   cloths 
                   WHERE  name like '%query%'));
于 2013-11-07T20:38:52.100 回答