从哪里字段中选择不等于Mysql Php

时间:2010-07-20 02:47:56

标签: php mysql sql database select

我只是想知道我可以在php中执行什么样的mysql命令,它会从某个表中选择所有项目,其中columna不等于x且columnb不等于x

类似于:select something from table where columna does not equal x and columnb does not equal x

有什么想法吗?

6 个答案:

答案 0 :(得分:68)

关键是sql查询,您将设置为字符串:

$sqlquery = "SELECT field1, field2 FROM table WHERE NOT columnA = 'x' AND NOT columbB = 'y'";

请注意,有很多方法可以指定NOT。另一个同样有效的是:

$sqlquery = "SELECT field1, field2 FROM table WHERE columnA != 'x' AND columbB != 'y'";

以下是如何使用它的完整示例:

$link = mysql_connect($dbHost,$dbUser,$dbPass) or die("Unable to connect to database");
mysql_select_db("$dbName") or die("Unable to select database $dbName");
$sqlquery = "SELECT field1, field2 FROM table WHERE NOT columnA = 'x' AND NOT columbB = 'y'";
$result=mysql_query($sqlquery);

while ($row = mysql_fetch_assoc($result) {
//do stuff
}

你可以在上面的while循环中做任何你想做的事情。访问表格的每个字段作为$row array的元素,这意味着$row['field1']将为您提供当前行field1的值,$row['field2']将为您提供field2的值。

请注意,如果列可能具有NULL值,则使用上述任一语法都无法找到这些值。您需要添加子句以包含NULL值:

$sqlquery = "SELECT field1, field2 FROM table WHERE (NOT columnA = 'x' OR columnA IS NULL) AND (NOT columbB = 'y' OR columnB IS NULL)";

答案 1 :(得分:7)

您可以使用

import UIKit

class ViewController: UIViewController {


    @IBOutlet weak var tests_label: UILabel!

    var databasePath = NSString()

    override func viewDidLoad() {
        super.viewDidLoad()

        let filemgr = NSFileManager.defaultManager()

        let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)

        let docsDir = dirPaths[0] as! String

        databasePath = docsDir.stringByAppendingPathComponent("vmd_db.db")

        let myDatabase = FMDatabase(path: databasePath as String)

        if myDatabase.open(){

            var arrayData:[String] = []

            let query_lab_test = "SELECT lab_test FROM lab_test"

            let results_lab_test:FMResultSet? = myDatabase.executeQuery(query_lab_test, withArgumentsInArray: nil)

            while results_lab_test?.next() == true {

                if let resultString = results_lab_test?.stringForColumn("lab_test"){

                arrayData.append(resultString)

            }
        }
            var multiLineString = join("\u{0085}", arrayData)
            tests_label.text = multiLineString
            tests_label.numberOfLines = 0
            tests_label.lineBreakMode = NSLineBreakMode.ByWordWrapping
            tests_label.sizeToFit()
            //println(arrayData)
            myDatabase.close()
    }
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

或者

NOT columnA = 'x'

或者

columnA != 'x'

和Jeffly Bake的查询一样,对于包含空值,你不必像

那样写
columnA <> 'x'

你可以通过

简化
(NOT columnA = 'x' OR columnA IS NULL)

&lt; =&gt; 是Null Safe等于Operator,其中包含偶数空值的结果。

答案 2 :(得分:1)

select * from table where fiels1 NOT LIKE 'x' AND field2 NOT LIKE 'y'

//这项工作以不区分大小写的方式

答案 3 :(得分:1)

$sqlquery = "SELECT field1, field2 FROM table WHERE columnA <> 'x' AND columbB <> 'y'";

我建议使用菱形运算符(&lt;&gt;)来支持!=,因为第一个是有效的SQL,第二个是MySQL的添加。

答案 4 :(得分:1)

或者也可以在括号内插入语句。

SELECT * FROM tablename WHERE NOT (columnA = 'x')

答案 5 :(得分:-1)

您也可以使用

select * from tablename where column1 ='a' and column2!='b';