像表达式一样,mysql不起作用

时间:2016-06-30 14:01:19

标签: mysql

我遇到了MySQL查询问题。当我尝试使用查询的其中一列过滤查询时,它表示没有行。

SELECT `Voucher`.`id`, `Voucher`.`name`, `Voucher`.`batch`, `Voucher`.`status`, `Voucher`.`perc_time_used`, `Voucher`.`perc_data_used`, 
`Voucher`.`last_accept_time`, `Voucher`.`last_reject_time`, `Voucher`.`last_accept_nas`, `Voucher`.`last_reject_nas`, `Voucher`.`last_reject_message`, 
`Voucher`.`user_id`, `Voucher`.`created`, `Voucher`. `modified`, `Voucher`.`extra_name`, `Voucher`.`extra_value`, `Voucher`.`password`, 
`Voucher`.`realm`, `Voucher`.`realm_id`, `Voucher`.`profile`, `Voucher`.`profile_id`, `Voucher`.`expire`, `Voucher`.`time_valid`, `Voucher`.`data_used`, 
`Voucher`.`data_cap`, `Voucher`.`time_used`, `Voucher`.`time_cap`, 
(SELECT voucher_values.value FROM voucher_values WHERE voucher_values.attribute = 'gender' AND voucher_values.voucher_id = `Voucher`.`id`) AS `gender`, 
(SELECT voucher_values.value FROM voucher_values WHERE voucher_values.attribute = 'residence' AND voucher_values.voucher_id = `Voucher`.`id`) AS `residence`, 
(SELECT voucher_values.value FROM voucher_values WHERE voucher_values.attribute = 'age' AND voucher_values.voucher_id = `Voucher`.`id`) AS `age`, 
(SELECT voucher_values.value FROM voucher_values WHERE voucher_values.attribute = 'nationality' AND voucher_values.voucher_id = `Voucher`.`id`) AS `nationality`, 
(SELECT voucher_values.value FROM voucher_values WHERE voucher_values.attribute = 'email' AND voucher_values.voucher_id = `Voucher`.`id`) AS `email`, 
(SELECT voucher_values.value FROM voucher_values WHERE voucher_values.attribute = 'facebookid' AND voucher_values.voucher_id = `Voucher`.`id`) AS `facebookid`, 
(SELECT voucher_values.value FROM voucher_values WHERE voucher_values.attribute = 'googleid' AND voucher_values.voucher_id = `Voucher`.`id`) AS `googleid`, 
(SELECT voucher_values.value FROM voucher_values WHERE voucher_values.attribute = 'interests' AND voucher_values.voucher_id = `Voucher`.`id`) AS `interests`, 
`User`.`id`, `User`.`username`, `User`.`password`, `User`.`token`, `User`.`name`, `User`.`surname`, `User`.`address`, `User`.`phone`, `User.email`,
`User`.`active`, `User`.`monitor`, `User`.`country_id`, `User`.`group_id`, `User`.`language_id`, `User`.`parent_id`, `User`.`lft`, `User`.`rght`, 
`User`.`created`, `User`.`modified` 
FROM `rd`.`vouchers` AS `Voucher` LEFT JOIN `rd`.`users` AS `User` ON (`Voucher`.`user_id` = `User`.`id`)
WHERE `User`.`id` IN (58,59) AND `email` LIKE '%%' ORDER BY Voucher.name DESC LIMIT 100 OFFSET 0

我的问题出在WHERE条款上 - 更具体地说是email LIKE '%%'。当我尝试在百分比之间放置一些东西时,查询不返回任何行。而且我知道我投入的角色就是那个领域。

2 个答案:

答案 0 :(得分:1)

我不知道是否是导致问题的原因,但是LEFT JOIN时,LEFT JOIN的右表上的条件只应在{ON内指定1}}子句。

FROM rd.vouchers AS Voucher
LEFT JOIN rd.users AS User 
ON (Voucher.user_id = User.id AND User.id IN (58,59))
WHERE email LIKE '%String%'
ORDER BY Voucher.name DESC
 LIMIT 100 OFFSET 0

如果Email列属于users表,那么这就是问题所在,您还应该将其添加到ON子句中:

ON (Voucher.user_id = User.id
    AND User.id IN (58,59)
    AND email LIKE '%String%')
ORDER BY Voucher.name DESC
 LIMIT 100 OFFSET 0

答案 1 :(得分:0)

处于此状态时会返回多少行? (没有LIMIT) 你在%%之间的角色是什么?