简体   繁体   English

如何在SQL的一个字段中搜索某个部分?

[英]how to search a certain part in one field on SQL?

I'm doing some task using Codeigniter and i have a problem in query search.我正在使用 Codeigniter 执行一些任务,但在查询搜索中遇到问题。 I have the following data in mysql:我在mysql中有以下数据:

order_billing
--------------
19001-00001-32222
13501-00021-31122
13344-02351-16072
18701-00001-30922
11123-12301-12122

it have 3 parts format for order_billing : number_1-number_2-number_3 order_billing 有 3 部分格式:number_1 number_1-number_2-number_3 number_2 number_1-number_2-number_3

and i make search form for like this我像这样制作搜索表格

<input type="text" name="order_billing_1">-
<input type="text" name="order_billing_2">-
<input type="text" name="order_billing_3"> 

how do i can make a SQL query when for with condition有条件时如何进行 SQL 查询

  • input "order_billing_1" for search "number_1"输入“order_billing_1”搜索“number_1”
  • input "order_billing_2" for search "number_2"输入“order_billing_2”搜索“number_2”
  • input "order_billing_3" for search "number_3"输入“order_billing_3”搜索“number_3”

i made some code like this我做了一些这样的代码

$post = $this->input->post();
$search['number_1'] = $post['order_billing_1'];
$search['number_2'] = "-".$post['order_billing_2'];
$search['number_3'] = "-".$post['order_billing_3'];

and the query like this和这样的查询

$this->db->like('order_billing', '$search['number_1']', 'after');
$this->db->or_like('order_billing', '$search['number_2']', 'both');
$this->db->or_like('order_billing', '$search['number_3']', 'before');

but it still not correct.但它仍然不正确。 i still confuse how to search like that in manual SQL query.我仍然困惑如何在手动 SQL 查询中进行这样的搜索。

$search['number_1'] = $post['order_billing_1'];
$search['number_2'] = "-".$post['order_billing_2'];
$search['number_3'] = "-".$post['order_billing_3'];

the query查询

Select * from TABLE WHERE order_billing like ????

do any one know how to solve this.?有谁知道如何解决这个问题。?

Why don't you make it into one long string?你为什么不把它做成一根长绳?

$order_billing = $post['order_billing_1']."-".
                 $post['order_billing_2']."-".
                 $post['order_billing_3'];

You can then use this in a simple query.然后您可以在一个简单的查询中使用它。

Please note I used $post and not $_POST to mimic what you did.请注意,我使用$post而不是$_POST来模仿您所做的。

In plain SQL you want在你想要的普通 SQL 中

SELECT * FROM TABLE WHERE order_billing = CONCAT('13344','-','02351','-','16072')

order_billing is a single column so needs to be searched with a single string. order_billing是单列,因此需要使用单个字符串进行搜索。 CONCAT() puts your three input fields into a single string. CONCAT()将您的三个输入字段放入一个字符串中。 There are plenty of other ways to do that too.还有很多其他方法可以做到这一点。

Beware SQL injection.当心 SQL 注入。 And think about normalization.并考虑规范化。

like '%something%' searches 'something' between some other text like '%something%' ”在其他文本之间搜索“something”

let's say you are searching for '1234'假设您正在搜索“1234”

for order billing 1 use -> like '1234-%' for order billing 2 use -> like '%-1234-%' for order billing 3 use -> like '%-1234'对于订单计费 1 使用 -> like '1234-%'用于订单计费 2 使用 -> like '%-1234-%'用于订单计费 3 使用 -> like '%-1234'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM