有and 有OR的依据几个字段生成SQL语句
有and 有OR的根据几个字段生成SQL语句
有如下图的数据表信息,有什么好的方法把它们组合成一条SQL?如果全是and 的或者全是or的,这方法好办,可是两个同时都有的,怎么办呢?
全是AND 或OR的话
- PHP code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> $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'].' '; }
------解决方案--------------------
- SQL code
select * from sk_cvfilter where cvid=58 and columnname!='' and andor='and' or andor='or' <br><font color="#e78608">------解决方案--------------------</font><br>你的这个记录有问题,为什么呢?<br><br>例如出现一个 (a = 1 or (b=2 and b=3)),这个时候,你是怎么记录的?<br><br>我猜是这样的<br><br>columnname comparator value andor<br>a eq 1 or<br>b eq 2 and<br>c eq 3 <br><br>这个时候问题出现了,假如你不知道上面的那个关系式(a = 1 or (b=2 and b=3)),<br>如果只从结构看的话,你是认为<br><br>(a = 1 or b =2 ) and b = 3<br><br>还是<br><br> a = 1 or (b=2 and b=3)<br><br>上述问题不考虑,所有or为一个条件和其他条件and的写法<br><br><br>
- PHP code
<?php $sql = "select * from sk_cvfilter where cvid=58 and columnname!=''"; $query = mysql_query($sql); $sql = ''; // 记录上一句的是不是or $is_last_or = false; while($row = mysql_fetch_assoc($query)){ if($row['comparator']=='eq'){$comparator="=";} else{$comparator=$row['comparator'];} if($is_last_or) { if($row['andor'] == 'and') { // 和下一句是and关系说明括号要结束了 $sql .=$row['columnname'].$comparator."'".$row['value']."' ) ".$row['andor'].' '; } else { // 持续括号里的内容 $sql .=$row['columnname'].$comparator."'".$row['value']."' ".$row['andor'].' '; } } elseif($row['andor'] == 'or') { // 如果有or则意味着和下一句有或的关系(因为你这里没有括号) $is_last_or = true; // 加上括号吧 $sql .= '( ' . $row['columnname'].$comparator."'".$row['value']."' ".$row['andor'].' '; } else { $sql .=$row['columnname'].$comparator."'".$row['value']."' ".$row['andor'].' '; } } <br /><font color="#e78608">------解决方案--------------------</font><br>
- PHP code
<?php $sql = "select * from sk_cvfilter where cvid=58 and columnname!=''"; $query = mysql_query($sql); $sql = ''; // 记录上一句的是不是or $is_last_or = false; while($row = mysql_fetch_assoc($query)){ if($row['comparator']=='eq'){$comparator="=";} else{$comparator=$row['comparator'];} if($is_last_or) { if($row['andor'] != 'or') { // 和下一句是and或者结束了关系说明括号要结束了 $sql .=$row['columnname'].$comparator."'".$row['value']."' ) ".$row['andor'].' '; } else { // 持续括号里的内容 $sql .=$row['columnname'].$comparator."'".$row['value']."' ".$row['andor'].' '; } } elseif($row['andor'] == 'or') { // 如果有or则意味着和下一句有或的关系(因为你这里没有括号) $is_last_or = true; // 加上括号吧 $sql .= '( ' . $row['columnname'].$comparator."'".$row['value']."' ".$row['andor'].' '; } else { $sql .=$row['columnname'].$comparator."'".$row['value']."' ".$row['andor'].' '; } } <div class="clear">

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

1. Sorting and deduplication In daily work, there are always some scenarios that require some filtering of the result set. For example, the result set obtained after interacting with a third party needs to be sorted and deduplicated again. In this case, the result set will be deduplicated according to a certain field, or sorted by a certain field. In Java, when it comes to deduplication, we can easily think of the characteristics of Set (unordered, no duplication), and TreeSet (ordered, no duplication) can also specify deduplication rules (generally ascending result sets after deduplication) . When it comes to sorting, we can easily think of various sorting algorithms, but Java already provides sorting functions, such as the sort() method in collections, and you can also specify the sorting fields and ascending and descending order. Let me say one more thing here, the characteristics of Set (no order and no duplication): disorder: disorder

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Comparable and ComparatorComparable and Comparator are two Java sorting-related interfaces, also known as natural sorting and custom sorting. I have recently read relevant content, and now I will record my own learning status below. Comparable and Comparator are two interfaces about sorting, used to implement the sorting function in Java collections. The specific functions can be obtained by viewing the API. Comparable Here's a brief introduction from the API documentation: Thisinterfaceimposesatotalorderingontheobjectsofeachclassthatimp

1. Explain that Java provides a comparison interface Comparable for comparison. All classes that implement this interface dynamically implement this comparison method. In fact, Java not only provides a comparison interface, but also provides another interface. The Comparator interface also has a comparison function, but this interface focuses on comparing containers. 2. Instance Comparator was widely used before Java8. Java8 not only upgrades to functional interfaces, but also extends default methods. Comparatorcomparator=(p1,p2)->p1.firstName.compareTo(p2.firstName);Personp1=ne

不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没问题。

图片消失如何解决先是图片文件上传$file=$_FILES['userfile']; if(is_uploaded_file($file['tmp_name'])){$query=mysql_query("INSERT INTO gdb_banner(image_src ) VALUES ('images/{$file['name'

图片消失如何解决先是图片文件上传$file=$_FILES['userfile']; if(is_uploaded_file($file['tmp_name'])){$query=mysql_query("INSERT INTO gdb_banner(image_src ) VALUES ('images/{$file['name'
