搜索Concat字符串而不是worknig

时间:2015-11-20 20:17:42

标签: mysql sql

我有这个查询,我用它来搜索客户的姓氏,工作正常:

SELECT `pt_bookings`.`booking_id`,`pt_bookings`.`booking_date`,
pt_bookings.booking_ref_no, `ai_last_name`,`pt_bookings`.`booking_checkout`,`pt_bookings`.`booking_checkin` FROM (`pt_bookings`) INNER join pt_accounts on pt_accounts.accounts_id = pt_bookings.accounts_id AND pt_accounts.accounts_type='customers'WHERE ai_last_name LIKE CONCAT('%jansen%')

但是我想搜索一串3个连接变量ai_firstname ai_middle_name ai_last_name,我会优先使用这个查询,但它返回0条记录并且没有任何错误。这个查询有什么问题? Txs!

SELECT `pt_bookings`.`booking_id`,`pt_bookings`.`booking_date`,  pt_bookings.booking_ref_no, CONCAT(`pt_accounts`.`ai_first_name`,`pt_accounts`.`ai_middle_name`,`pt_accounts`.`ai_last_name`) as customer_name,  `pt_bookings`.`booking_checkout`,`pt_bookings`.`booking_checkin`FROM (`pt_bookings`) INNER join pt_accounts on pt_accounts.accounts_id = pt_bookings.accounts_id AND pt_accounts.accounts_type='customers'WHERE  customer_name LIKE  CONCAT('%jansen%')

2 个答案:

答案 0 :(得分:2)

这是您的查询:

SELECT b.`booking_id`, b.`booking_date`,  b.booking_ref_no, 
       CONCAT(`a.`ai_first_name`, a.`ai_middle_name`, a.`ai_last_name`) as customer_name,
      b.`booking_checkout`, b.`booking_checkin`
FROM pt_bookings b INNER join
     pt_accounts a
     on a.accounts_id = b.accounts_id AND
        a.accounts_type = 'customers'
WHERE customer_name LIKE  CONCAT('%jansen%');

主要问题是customer_name是列别名,因此您无法在WHERE中使用它。 MySQL有一个方便的扩展,允许在HAVING子句中使用这个逻辑:

HAVING customer_name LIKE '%jansen%'

我不确定CONCAT()的含义。没有理由将它与一个参数一起使用。

答案 1 :(得分:0)

试试这个:

SELECT b.`booking_id`, b.`booking_date`,  b.booking_ref_no, 
       CONCAT(`a.`ai_first_name`, a.`ai_middle_name`, a.`ai_last_name`) as customer_name,
      b.`booking_checkout`, b.`booking_checkin`
FROM pt_bookings b INNER join
     pt_accounts a
     on a.accounts_id = b.accounts_id AND
        a.accounts_type = 'customers'
WHERE
  CONCAT(ai_firstname, ai_middle_name, ai_last_name)
  LIKE '%jansen%';