Generate an SQL statement based on several fields with and OR

Source: Internet
Author: User
If there is and OR, an SQL statement is generated based on several fields, such as data table information. how can I combine them into an SQL statement? If all is and or all is or, this method is easy to handle, but both are available at the same time, what should we do? PHPcode $ SQL & quot; select * fromsk_cvfilterwherecvid58andcolu generate an SQL statement based on several fields if they are all AND OR
Is there any good way to combine data table information into an SQL statement? If all is and or all is or, this method is easy to handle, but both are available at the same time, what should we do?



All are and or.

PHP code
          $sql = "select * from sk_cvfilter where cvid=58 and columnname!=''";        $query = mysql_query($sql);        $sql = '';        while($row = mysql_fetch_assoc($query)){            if($row['comparator']=='eq'){$comparator="=";}            else{$comparator=$row['comparator'];}            $sql .=$row['columnname'].$comparator."'".$row['value']."' ".$row['andor'].' ';        }


------ Solution --------------------
SQL code
Select * from sk_cvfilter where cvid = 58 and columnname! = ''And andor = 'and' or andor = 'OR'
------ Solution --------------------
Why is there a problem with your record?

For example, if a (a = 1 or (B = 2 and B = 3) appears, how do you record it?

I guess so.

Columnname comparator value andor
A eq 1 or
B eq 2 and
C eq 3

This problem occurs. if you do not know the above relation (a = 1 or (B = 2 and B = 3 )),
If you only look at the structure, you think

(A = 1 or B = 2) and B = 3

Or

A = 1 or (B = 2 and B = 3)

All or statements for one condition and other conditions and are not considered.


PHP code
    ------ Solution --------------------
PHP code
      

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.