json 上的 HAVING 或 WHERE ?

发布于 2024-12-11 00:10:59 字数 1118 浏览 0 评论 0原文

我想用 json_encode 存储在数据库中的值 json 完成“HAVING”,这个值是波斯语单词,但没有完成“HAVING”,如何修复它?

row_s 如下:

row_s id 1:["All5","\u0648\u06cc\u0632\u0627 8"]
row_s id 2:["All5","\u0648\u06cc\u0632\u0627 7"]
row_s id 3: ["All5","\u0648\u06cc\u0632\u0627 6"]

这是我的 php 代码:(输出是: <强>没有)

$val   = 'ویزا 8';
$query = $this->db->query('SELECT * FROM table HAVING row_s LIKE "%' . $val . '%"');
if ($query->num_rows() > 0) {
    foreach ($query->result() as $val) {
        echo $val->name . '<br>';
    }
} else {
    echo 'There is not';
}

更新: 可以将数据库中值为 json 的值“$val”指定为数字或拉丁词,但不能指定波斯语词。

例如,以下代码有输出,并且是 $query->num_rows()=3。

$val   = 'All5';
$query = $this->db->query('SELECT * FROM table HAVING row_s LIKE "%' . $val . '%"');
if ($query->num_rows() > 0) {
    foreach ($query->result() as $val) {
        echo $val->name . '<br>';
    }
} else {
    echo 'There is not';
} 

I want done "HAVING" for value json that store in database with json_encode, this values is persian word but but not done "HAVING" for it, how is fix it?

row_s is as:

row_s id 1: ["All5","\u0648\u06cc\u0632\u0627 8"]
row_s id 2: ["All5","\u0648\u06cc\u0632\u0627 7"]
row_s id 3: ["All5","\u0648\u06cc\u0632\u0627 6"]

This is my php code:(output it is: There is not)

$val   = 'ویزا 8';
$query = $this->db->query('SELECT * FROM table HAVING row_s LIKE "%' . $val . '%"');
if ($query->num_rows() > 0) {
    foreach ($query->result() as $val) {
        echo $val->name . '<br>';
    }
} else {
    echo 'There is not';
}

Update:
Can where value “$val” that is number or latin word with value json in database, but can not for persian word.

for example, following code have output, and is $query->num_rows()=3.

$val   = 'All5';
$query = $this->db->query('SELECT * FROM table HAVING row_s LIKE "%' . $val . '%"');
if ($query->num_rows() > 0) {
    foreach ($query->result() as $val) {
        echo $val->name . '<br>';
    }
} else {
    echo 'There is not';
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随遇而安 2024-12-18 00:10:59

您已将 json 字符串存储到数据库中。这样做只是将文本存储到数据库中。

SQL 是一种用于查询(以及更多)数据库的语言,例如从数据库中选择文本字段。

但是 SQL 不了解 json,特别是 LIKE 关键字无法解析您的 json,然后对其应用常规的、基于文本的模式匹配。

您需要以纯文本形式存储数据(跨多个列)或者您需要查询所有字段,然后使用 PHP 搜索数据。

第三种选择是扩展数据库服务器以理解 json 并提供一个功能,允许您在 json 编码数据中进行类似于 LIKE 的搜索。

然而,事实上,您刚刚在数据库中创建了一个数据库,该数据库的价格就像您现在面临的选择所需数据的问题一样。

You have stored a json string into your database. Doing so, is just storing text into the database.

SQL is a language to query (and more) the database, e.g. to select text fields from the database.

But SQL does not know about json, especially, the LIKE keyword is not able to parse your json and then apply it's regular, text based pattern matching on it.

You need to either store your data in plain text (across multiple columns) or you need to query all fields and then use PHP to search through your data.

A third alternative is to extend your database server to understand json and to provide a function that allows you to search in json encoded data similar to LIKE.

However, infact you have just created a database inside the database which comes with the price like the problem you're now facing to select the data you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文