I have this query that I use for searching customer's last names which is working fine:
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%')
But I would like to search on a string of 3 concatenated varchars ai_firstname ai_middle_name ai_last_name and I would prefer to use this query but it returns 0 records and does not throw any error. What is wrong about this query? Thanks!
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%')
This is your query:
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%');
The main problem is that customer_name
is a column alias, so you cannot use it in a WHERE
. MySQL has a convenient extension, that allows this logic in a HAVING
clause:
HAVING customer_name LIKE '%jansen%'
I'm not sure what the CONCAT()
is for. There is no reason to use it with one argument.