仅在列符合条件的情况下进行多列搜索

时间:2018-03-14 15:48:35

标签: mysql sql

我正在制作搜索功能。我想搜索几个选定列已赋值的位置,但必须限制在user_id='x'的位置。我所拥有的所有结果,而不仅仅是user_id='x',这就是我想要的结果:

在我的查询下方,它会返回所有内容而不仅仅是user_id='x'

的搜索数据
SELECT * FROM request 
WHERE product_name LIKE 'NDQB12%' OR request_id LIKE 'NDQB12%' 
OR batch_no LIKE 'NDQB12%' AND user_id='16';

建议

4 个答案:

答案 0 :(得分:3)

您需要使用括号来分隔条件:

<input  type="title" id="topictitile">
<input  type="title" id="title" name="title[]">
<textarea  type="text" rows= "5" id="content" name="content[]"></textarea>
<input type="file" id="fileButton" name="fileButton[]" /> 
<input type="file" id="fileButton" name="fileButton[]" />  //i have no clue here how to get file and put into database
<button id="add-data" >summit</button>

        <script>
        document.getElementById("add-data").onclick = function(e) {
              var category = document.getElementById('category').value;
              var topictitle = document.getElementById('topictitile').value;
              var topic = document.getElementsByName('title[]');
               var content = document.getElementsByName('content[]');



        for (var i = 0; i <topic.length; i++) {


        var tp=topic[i];
        var ct=content[i];
        var storesRef = rootRef.child('ProcessForQuit/'+category+'/'+topictitle+'/'+i);
                     storesRef.set({
                     topicname: tp.value,
                     detail: ct.value,
                  picturepath : thumbnail //i try to add path with file to database and storage

        });
        }
        alert("Success");
        }
        </script>

此查询仅显示用户SELECT * FROM request WHERE ( (product_name LIKE 'NDQB12%') OR (request_id LIKE 'NDQB12%') OR (batch_no LIKE 'NDQB12%') ) AND user_id='16'; 的数据,以及之前的某个条件是否为True。

答案 1 :(得分:1)

尝试这样的事情。我认为你没有把你的情况包好。

 SELECT * FROM request 
    WHERE user_id='16'
    AND (product_name LIKE 'NDQB12%' 
    OR request_id LIKE 'NDQB12%' 
    OR batch_no LIKE 'NDQB12%' )

答案 2 :(得分:1)

您的逻辑操作之间存在优先级问题。在SQL和大多数编程语言中,当混合使用AND和OR(以及其他逻辑运算符)逻辑运算符时,始终建议将它们与括号分开。对于你的情况:

SELECT 
    * 
FROM 
    request 
WHERE 
    user_id='16' AND
    (
        product_name LIKE 'NDQB12%' OR 
        request_id LIKE 'NDQB12%' OR 
        batch_no LIKE 'NDQB12%'
    )

答案 3 :(得分:0)

void MyServer::httphandle(char* buffer, int socket){
    char* requestbuffer = buffer;
    std::stringstream datastream,headerstream;

    auto requestline = strtok(requestbuffer,"\n");
    auto header = strtok(nullptr,"\n");
    headerstream << header << "\n";

    while((header != nullptr) && (strlen(header)>1)){
        header = strtok(nullptr,"\n");
        headerstream << header <<"\n";
    }

    auto data = strtok(nullptr,"\n");
    char* reqline[3];

    reqline[0] = strtok (requestline, " \t\n");
    if(routemap.find(reqline[1]) != routemap.end()){
                    auto reponsestring = routemap[reqline[1]](data);
                    write(socket, "HTTP/1.0 200 OK\n\n", 17);
                    write(socket, reponsestring.c_str(), strlen(reponsestring.c_str()));
            }
            else    
                write(socket, "HTTP/1.0 404 Not Found\n\n{\"error\":\"Not Found\"}\n", strlen("HTTP/1.0 404 Not Found\n\n{\"error\":\"Not Found\"}\n")); //FILE NOT FOUND
}

你能试试这个吗?