简体   繁体   English

mysql搜索多个字段

[英]mysql search for more than one field

Hi guys I didn't find anything useful on stack overflow about the following problem. 嗨,大家好,我没有发现有关以下问题的任何有用信息。

I want to search a customer for more than one field. 我想搜索一个客户的多个字段。 I have some textboxes where I can enter my search criteria. 我有一些文本框可以输入搜索条件。

customers table: name surname job abcaacaaa 客户表:姓名姓工作abcaacaaa

eg, when I search for name = a, surname = b, job = c, I want to see my first customer, if I search for name = a, I want to see every customer (the point is, if surname and job are null I can't get the name back from the query) 例如,当我搜索名称= a,姓氏= b,工作= c时,我想看到我的第一个客户,如果我搜索名称= a,则想看到每个客户(关键是,如果姓氏和工作是null我无法从查询中获取名称)

I want to search for the customers in my stored procedure, which has to fill a gridview. 我想在我的存储过程中搜索客户,该过程必须填充gridview。

C# code for my search button: 我的搜索按钮的C#代码:

protected void btnRicerca_Click(object sender, EventArgs e)
{
    GridView1.Visible = true;


    ClienteInfo CI = new ClienteInfo();
    DboUser objdbo = new DboUser();
    int id_cliente = 0;

    txtNomeCliente.Text = CI.nome_cliente;
    txtCognomeCliente.Text = CI.cognome_cliente;
    txtRagioneSociale.Text = CI.ragione_sociale;
    txtVia.Text = CI.via;
    txtCitta.Text = CI.citta;
    txtProvincia.Text = CI.provincia;
    txtCap.Text = CI.cap;
    txtNazione.Text = CI.nazione;
    txtTelefono.Text = CI.recapito_telefonico;
    txtPartitaIva.Text = CI.partita_iva;
    txtCodiceFiscale.Text = CI.codice_fiscale;
    txtEmail.Text = CI.email;
    txtPubblicaAmministrazione.Text = CI.codice_pubblica_amministrazione;
    CI.id_utente = Convert.ToInt64(Session["Id_Utente"]);

    id_cliente = objdbo.getClienteInfo(CI);
    }

It seems you have to create the SQL Query according to the UI input values. 看来您必须根据UI输入值创建SQL查询。 As an example if some one enters values for name and surname then your SQL Query should be like 例如,如果有人输入名称和姓氏值,那么您的SQL查询应该像

SELECT * FROM customers WHERE name = "a" and surname = "b"; 

And if Someone enters only the name your SQL Query should be as following. 并且,如果有人仅输入名称,则SQL查询应如下所示。

SELECT * FROM customers WHERE name = "a";

Ideally You should manage the SQL Query from your application. 理想情况下,您应该从应用程序中管理SQL查询。 I faced a similar experience and I used the Interpreter pattern to Generate The SQL Query. 我遇到了类似的经历,并且我使用了解释器模式来生成SQL查询。

Additionally it's always better to use like search for names. 另外,最好使用类似搜索名称的方式。

ex : SELECT * FROM customers WHERE name like "%a%" 例如: SELECT * FROM customers WHERE name like "%a%"

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

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