sql中的内连接和Like子句

时间:2014-03-23 15:01:24

标签: sql

我想在我的内部联接声明中使用Like进行搜索。

搜索:

 SELECT * 
 FROM CARS 
 WHERE (plate_no LIKE '%$search%' OR eng_no LIKE '%$search%' 
        or chass_no like '%$search%' or car_year  LIKE '%$search%' 
        or car_brand LIKE '%$search%' or car_model LIKE '%La%' 
        or horse_power LIKE '%$search%') 
   AND status='sold'

内部联接:

  SELECT cars.
     plate_no, cars.car_condition, cars.photo_path, cars.car_year, 
     cars.car_brand, cars.car_model, transaction.actual_price, customer_maintenance.id
  FROM 
     `cars` 
  INNER JOIN 
     transaction ON (cars.id = transaction.car_id)
  INNER JOIN 
     customer_maintenance ON (transaction.customer_id = customer_maintenance.id) 
  WHERE
     status = 'sold'

2 个答案:

答案 0 :(得分:0)

你在寻找这样的东西吗?:

SELECT cars.plate_no, 
       cars.car_condition, 
       cars.photo_path, 
       cars.car_year, 
       cars.car_brand, 
       cars.car_model, 
       transaction.actual_price, 
       customer_maintenance.id 
FROM   cars 
       INNER JOIN transaction 
               ON transaction.car_id = cars.id 
       INNER JOIN customer_maintenance 
               ON transaction.customer_id = customer_maintenance.id 
WHERE  cars.status = 'sold' 
       AND ( cars.plate_no LIKE '%$search%' 
              OR cars.eng_no LIKE '%$search%' 
              OR cars.chass_no LIKE '%$search%' 
              OR cars.car_year LIKE '%$search%' 
              OR cars.car_brand LIKE '%$search%' 
              OR cars.car_model LIKE '%La%' 
              OR cars.horse_power LIKE '%$search%' ) 

答案 1 :(得分:0)

通过将其替换为更复杂的表达式来扩展INNER JOIN子句的匹配表达式也是有效的:

SELECT *
FROM   cars 
   INNER JOIN transaction 
           ON transaction.car_id = cars.id AND car.brand like '%search%'
   INNER JOIN customer_maintenance 
           ON transaction.customer_id = customer_maintenance.id 
WHERE  cars.status = 'sold' 

将它放在WHERE子句中会产生相同的结果。