Mysql where 子句使用 equal 不起作用,只能使用 LIKE
Mysql where clause using equal is not working, only by using LIKE
我有一个 laravel 应用程序生成并执行下面的查询
select
*
from
`read_items`
where
`subject_type` = 'App\Models\Scholar'
and `user_id` = 59;
以上 sql 不起作用,尽管我在同一行有 user_id = 59
和 subject_type = 'App\Models\Scholar'
的数据
但是当我使用 like
时,查询有效
select
*
from
`read_items`
where
`subject_type` like '%Scholar'
and `user_id` = 59;
反斜杠在mysql中是一个转义字符,所以你必须加倍
select
*
from
`read_items`
where
`subject_type` = 'App\Models\Scholar'
and `user_id` = 59;
反斜杠\
用于转义下一个字符。所以你必须用一个 \
来逃避 \
所以你的查询应该是这样的
SELECT
*
FROM
`read_items`
WHERE
`subject_type` = 'App\Models\Scholar' AND `user_id` = 59
结果
But when I use like, the query is working
那是因为你只用了 subject_type like '%Scholar'
。尝试 subject_type LIKE '%Models\Scholar%'
,它不会工作
我有一个 laravel 应用程序生成并执行下面的查询
select
*
from
`read_items`
where
`subject_type` = 'App\Models\Scholar'
and `user_id` = 59;
以上 sql 不起作用,尽管我在同一行有 user_id = 59
和 subject_type = 'App\Models\Scholar'
的数据
但是当我使用 like
时,查询有效
select
*
from
`read_items`
where
`subject_type` like '%Scholar'
and `user_id` = 59;
反斜杠在mysql中是一个转义字符,所以你必须加倍
select
*
from
`read_items`
where
`subject_type` = 'App\Models\Scholar'
and `user_id` = 59;
反斜杠\
用于转义下一个字符。所以你必须用一个 \
\
所以你的查询应该是这样的
SELECT
*
FROM
`read_items`
WHERE
`subject_type` = 'App\Models\Scholar' AND `user_id` = 59
结果
But when I use like, the query is working
那是因为你只用了 subject_type like '%Scholar'
。尝试 subject_type LIKE '%Models\Scholar%'
,它不会工作