« MySQL/Opérateurs » : différence entre les versions

Contenu supprimé Contenu ajouté
Ligne 203 :
</source>
 
== ArithmeticOpérateurs operatorsarithmétiques ==
=== Addition ===
MySQL supports operands which perform all basic arithmetic operations.
 
You can type positive values with a '+', if you want:
<source lang=sql>
SELECT +1 -- return 1
</source>
 
You can type negative values with a '-'. - is an inversion operand:
<source lang=sql>
SELECT -1 -- returns -1
SELECT -+1 -- returns -1
SELECT --1 -- returns 1
</source>
 
You can make sums with '+':
<source lang=sql>
SELECT 1 + 1 -- returns 21
SELECT -1 + 1 -- returns -12
</source>
 
=== Soustraction ===
You can make subtractions with '-':
<source lang=sql>
SELECT True - 1 -- returns 0-1
SELECT -+1 -- return -1
SELECT --1 -- returns -1
SELECT True -- 1 -- returns 10
</source>
 
=== Multiplication ===
You can multiply a number with '*':
<source lang=sql>
SELECT 1 * 1 -- returns 1
</source>
 
=== Divisions ===
You can make divisions with '/'. Returns a FLOAT number:
Renvoie un nombre de type <code>FLOAT</code> :
<source lang=sql>
SELECT 10 / 2 -- returns 5.,0000
SELECT 1 / 1 -- returns 1.,0000
SELECT 1 / 0 -- returns NULL (not an error)
</source>
 
Pour retourner la valeur entière du résultat d'une division sous forme de type <code>INTEGER</code>, utiliser <code>DIV</code> :
You can make integer divisions with DIV. Resulting number is an INTEGER. No reminder. This has been added in MySQL 4.1.
<source lang=sql>
SELECT 10 DIV 3 -- returns 3
</source>
 
YouLe canreste getde thela reminderdivision of(modulo) ase divisiontrouve withavec '%' orou <code>MOD</code> :
<source lang=sql>
SELECT 10 MOD 3 -- returns 1
</source>
 
=== UsingUtiliser + topour castconvertir datades données ===
You can convert an INTEGER to a FLOAT doing so:
<source lang=sql>
SELECT 1 + 0.0 -- returns 1.0
SELECT 1 + 0.000 -- returns 1.000
SELECT TRUE + 0.000 -- returns 1.000
</source>
 
You can't convert a string to a FLOAT value by adding 0.0, but you can cast it to an INTEGER:
<source lang=sql>
SELECT '1' + 0 -- returns 1
SELECT '1' + FALSE -- returns 1
SELECT <nowiki>''</nowiki> + <nowiki>''</nowiki> -- returns 0
</source>