Anterior: Mysql conexões uso Show Seguinte: Expressões Regulares do MySQL Nós já sabemos MySQL usando o comando SQL SELECT com a cláusula WHERE para ler os dados na tabela de dados, mas os campos de critérios de consulta fornecido quando NULL, o comando pode não funcionar corretamente quando. Para lidar com este caso, MySQL fornece três principais operadores:
operação de comparação condição Sobre NULL é especial. Você não pode usar = NULL ou! = NULL localizar valores nulos na coluna. No MySQL, valores NULL comparar com qualquer outro valor (mesmo NULL) sempre retorna false, ou seja, NULL = NULL retorna false. MySQL o tratamento de NULL usando o IS NULL e IS NOT NULL operador. valores nulos em prompt de comandoO exemplo a seguir pressupõe que o banco de dados w3big tabelas tcount_tbl contendo dois w3big_author e w3big_count, w3big_count estabelecidos valores nulos de inserção. ExemplosTente os seguintes exemplos: [email protected]# mysql -u root -p password; Enter password:******* mysql> use w3big; Database changed mysql> create table tcount_tbl -> ( -> w3big_author varchar(40) NOT NULL, -> w3big_count INT -> ); Query OK, 0 rows affected (0.05 sec) mysql> INSERT INTO tcount_tbl -> (w3big_author, w3big_count) values ('mahran', 20); mysql> INSERT INTO tcount_tbl -> (w3big_author, w3big_count) values ('mahnaz', NULL); mysql> INSERT INTO tcount_tbl -> (w3big_author, w3big_count) values ('Jen', NULL); mysql> INSERT INTO tcount_tbl -> (w3big_author, w3big_count) values ('Gill', 20); mysql> SELECT * from tcount_tbl; +-----------------+----------------+ | w3big_author | w3big_count | +-----------------+----------------+ | mahran | 20 | | mahnaz | NULL | | Jen | NULL | | Gill | 20 | +-----------------+----------------+ 4 rows in set (0.00 sec) mysql>O exemplo a seguir você pode ver = e operador = não funciona !: mysql> SELECT * FROM tcount_tbl WHERE w3big_count = NULL; Empty set (0.00 sec) mysql> SELECT * FROM tcount_tbl WHERE w3big_count != NULL; Empty set (0.01 sec)Encontrar a tabela de dados w3big_count se a coluna é NULL, você deve usar o IS NULL e IS NOT NULL, os seguintes exemplos: mysql> SELECT * FROM tcount_tbl -> WHERE w3big_count IS NULL; +-----------------+----------------+ | w3big_author | w3big_count | +-----------------+----------------+ | mahnaz | NULL | | Jen | NULL | +-----------------+----------------+ 2 rows in set (0.00 sec) mysql> SELECT * from tcount_tbl -> WHERE w3big_count IS NOT NULL; +-----------------+----------------+ | w3big_author | w3big_count | +-----------------+----------------+ | mahran | 20 | | Gill | 20 | +-----------------+----------------+ 2 rows in set (0.00 sec)Use PHP valor NULL processamento do scriptscripts PHP você pode se ... else de lidar se a variável é vazia, e gera a declaração condicional correspondente. Os exemplos a seguir definido $ variável w3big_count PHP, em seguida, usar a variável com a tabela de dados w3big_count campos comparação: <?php $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } if( isset($w3big_count )) { $sql = 'SELECT w3big_author, w3big_count FROM tcount_tbl WHERE w3big_count = $w3big_count'; } else { $sql = 'SELECT w3big_author, w3big_count FROM tcount_tbl WHERE w3big_count IS $w3big_count'; } mysql_select_db('w3big'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not get data: ' . mysql_error()); } while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) { echo "Author:{$row['w3big_author']} <br> ". "Count: {$row['w3big_count']} <br> ". "--------------------------------<br>"; } echo "Fetched data successfully\n"; mysql_close($conn); ?>Anterior: Mysql conexões uso Seguinte: Expressões Regulares do MySQL
12.4.2 Comparison Functions and Operators
Table 12.4 Comparison Operators
Comparison operations result in a value of 1 (TRUE), 0 (FALSE), or NULL. These operations work for both numbers and strings. Strings are automatically converted to numbers and numbers to strings as necessary. The following relational comparison operators can be used to compare not only scalar operands, but row operands: = > < >= <= <> !=The descriptions for those operators later in this section detail how they work with row operands. For additional examples of row comparisons in the context of row subqueries, see Section 13.2.11.5, “Row Subqueries”. Some of the functions in this section return values other than 1 (TRUE), 0 (FALSE), or NULL. LEAST() and GREATEST() are examples of such functions; Section 12.3, “Type Conversion in Expression Evaluation”, describes the rules for comparison operations performed by these and similar functions for determining their return values.
Note In previous versions of MySQL, when evaluating an expression containing LEAST() or GREATEST(), the server attempted to guess the context in which the function was used, and to coerce the function's arguments to the data type of the expression as a whole. For example, the arguments to LEAST("11", "45", "2") are evaluated and sorted as strings, so that this expression returns "11". In MySQL 8.0.3 and earlier, when evaluating the expression LEAST("11", "45", "2") + 0, the server converted the arguments to integers (anticipating the addition of integer 0 to the result) before sorting them, thus returning 2. Beginning with MySQL 8.0.4, the server no longer attempts to infer context in this fashion. Instead, the function is executed using the arguments as provided, performing data type conversions to one or more of the arguments if and only if they are not all of the same type. Any type coercion mandated by an expression that makes use of the return value is now performed following function execution. This means that, in MySQl 8.0.4 and later, LEAST("11", "45", "2") + 0 evaluates to "11" + 0 and thus to integer 11. (Bug #83895, Bug #25123839) To convert a value to a specific type for comparison purposes, you can use the CAST() function. String values can be converted to a different character set using CONVERT(). See Section 12.11, “Cast Functions and Operators”. By default, string comparisons are not case-sensitive and use the current character set. The default is utf8mb4.
Page 2
Table 12.5 Logical Operators
In SQL, all logical operators evaluate to TRUE, FALSE, or NULL (UNKNOWN). In MySQL, these are implemented as 1 (TRUE), 0 (FALSE), and NULL. Most of this is common to different SQL database servers, although some servers may return any nonzero value for TRUE. MySQL evaluates any nonzero, non-NULL value to TRUE. For example, the following statements all assess to TRUE: mysql> SELECT 10 IS TRUE; -> 1 mysql> SELECT -10 IS TRUE; -> 1 mysql> SELECT 'string' IS NOT NULL; -> 1
Page 3
12.4.4 Assignment Operators
Table 12.6 Assignment Operators
Page 4
12.5 Flow Control Functions
Table 12.7 Flow Control Operators
The handling of system variable values by these functions changed in MySQL 8.0.22. For each of these functions, if the first argument contains only characters present in the character set and collation used by the second argument (and it is constant), the latter character set and collation is used to make the comparison. In MySQL 8.0.22 and later, system variable values are handled as column values of the same character and collation. Some queries using these functions with system variables that were previously successful may subsequently be rejected with Illegal mix of collations. In such cases, you should cast the system variable to the correct character set and collation. Page 5
|