1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983
|
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Functions and Operators</title><link rel="stylesheet" href="main.css" type="text/css" /><meta name="generator" content="DocBook XSL Stylesheets V1.69.1" /><link rel="start" href="index.html" title="Query Browser Help" /><link rel="up" href="index.html" title="Query Browser Help" /><link rel="prev" href="index.html" title="Query Browser Help" /><link rel="next" href="mysqlqb_statements.html" title="SQL Statement Syntax" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Functions and Operators</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="index.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="mysqlqb_statements.html">Next</a></td></tr></table><hr /></div><div class="chapter" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title"><a id="functions"></a>Functions and Operators</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="mysqlqb_functions.html#func-op-summary-ref">Operator and Function Reference</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#non-typed-operators">Operators</a></span></dt><dd><dl><dt><span class="section"><a href="mysqlqb_functions.html#operator-precedence">Operator Precedence</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#type-conversion">Type Conversion in Expression Evaluation</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#comparison-operators">Comparison Functions and Operators</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#logical-operators">Logical Operators</a></span></dt></dl></dd><dt><span class="section"><a href="mysqlqb_functions.html#control-flow-functions">Control Flow Functions</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#string-functions">String Functions</a></span></dt><dd><dl><dt><span class="section"><a href="mysqlqb_functions.html#string-comparison-functions">String Comparison Functions</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#regexp">Regular Expressions</a></span></dt></dl></dd><dt><span class="section"><a href="mysqlqb_functions.html#numeric-functions">Numeric Functions</a></span></dt><dd><dl><dt><span class="section"><a href="mysqlqb_functions.html#arithmetic-functions">Arithmetic Operators</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#mathematical-functions">Mathematical Functions</a></span></dt></dl></dd><dt><span class="section"><a href="mysqlqb_functions.html#date-and-time-functions">Date and Time Functions</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#mysql-calendar">What Calendar Is Used By MySQL?</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#fulltext-search">Full-Text Search Functions</a></span></dt><dd><dl><dt><span class="section"><a href="mysqlqb_functions.html#fulltext-boolean">Boolean Full-Text Searches</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#fulltext-query-expansion">Full-Text Searches with Query Expansion</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#fulltext-stopwords">Full-Text Stopwords</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#fulltext-restrictions">Full-Text Restrictions</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#fulltext-fine-tuning">Fine-Tuning MySQL Full-Text Search</a></span></dt></dl></dd><dt><span class="section"><a href="mysqlqb_functions.html#cast-functions">Cast Functions and Operators</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#other-functions">Other Functions</a></span></dt><dd><dl><dt><span class="section"><a href="mysqlqb_functions.html#bit-functions">Bit Functions</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#encryption-functions">Encryption and Compression Functions</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#information-functions">Information Functions</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#miscellaneous-functions">Miscellaneous Functions</a></span></dt></dl></dd><dt><span class="section"><a href="mysqlqb_functions.html#group-by-functions-and-modifiers">Functions and Modifiers for Use with <code class="literal">GROUP BY</code> Clauses</a></span></dt><dd><dl><dt><span class="section"><a href="mysqlqb_functions.html#group-by-functions"><code class="literal">GROUP BY</code> (Aggregate) Functions</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#group-by-modifiers"><code class="literal">GROUP BY</code> Modifiers</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#group-by-hidden-fields"><code class="literal">GROUP BY</code> and <code class="literal">HAVING</code> with Hidden
Fields</a></span></dt></dl></dd></dl></div><a id="id2811215" class="indexterm"></a><a id="id2811227" class="indexterm"></a><a id="id2811234" class="indexterm"></a><p>
Expressions can be used at several points in SQL statements, such as
in the <code class="literal">ORDER BY</code> or <code class="literal">HAVING</code>
clauses of <code class="literal">SELECT</code> statements, in the
<code class="literal">WHERE</code> clause of a <code class="literal">SELECT</code>,
<code class="literal">DELETE</code>, or <code class="literal">UPDATE</code> statement,
or in <code class="literal">SET</code> statements. Expressions can be written
using literal values, column values, <code class="literal">NULL</code>,
built-in functions, stored functions, user-defined functions, and
operators. This chapter describes the functions and operators that
are allowed for writing expressions in MySQL. Instructions for
writing stored functions and user-defined functions are given in
<a href="http://dev.mysql.com/doc/refman/5.0/en/stored-procedures.html" target="_top">Stored Procedures and Functions</a>, and
<a href="http://dev.mysql.com/doc/refman/5.0/en/adding-functions.html" target="_top">Adding New Functions to MySQL</a>. See
<a href="http://dev.mysql.com/doc/refman/5.0/en/function-resolution.html" target="_top">Function Name Parsing and Resolution</a>, for the rules describing how
the server interprets references to different kinds of functions.
</p><p>
An expression that contains <code class="literal">NULL</code> always produces
a <code class="literal">NULL</code> value unless otherwise indicated in the
documentation for a particular function or operator.
</p><p>
<span class="bold"><strong>Note</strong></span>: By default, there must be no
whitespace between a function name and the parenthesis following it.
This helps the MySQL parser distinguish between function calls and
references to tables or columns that happen to have the same name as
a function. However, spaces around function arguments are permitted.
</p><p>
You can tell the MySQL server to accept spaces after function names
by starting it with the <code class="option">--sql-mode=IGNORE_SPACE</code>
option. (See <a href="http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html" target="_top">SQL Modes</a>.) Individual client
programs can request this behavior by using the
<code class="literal">CLIENT_IGNORE_SPACE</code> option for
<code class="literal">mysql_real_connect()</code>. In either case, all
function names become reserved words.
</p><p>
For the sake of brevity, most examples in this chapter display the
output from the <span><strong class="command">mysql</strong></span> program in abbreviated
form. Rather than showing examples in this format:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT MOD(29,9);</code></strong>
+-----------+
| mod(29,9) |
+-----------+
| 2 |
+-----------+
1 rows in set (0.00 sec)
</pre><p>
This format is used instead:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT MOD(29,9);</code></strong>
-> 2
</pre><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="func-op-summary-ref"></a>Operator and Function Reference</h2></div></div></div><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
This table is part of an ongoing process to expand and simplify
the information provided on these elements. Further improvements
to the table, and corresponding descriptions will be applied
over the coming months.
</p></div><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><thead><tr><th><span class="bold"><strong>Name</strong></span></th><th><span class="bold"><strong>Description</strong></span></th></tr></thead><tbody><tr><td><a href="mysqlqb_functions.html#function_abs"><code class="literal">ABS()</code></a></td><td>Return the absolute value</td></tr><tr><td><a href="mysqlqb_functions.html#function_acos"><code class="literal">ACOS()</code></a></td><td>Return the arc cosine</td></tr><tr><td><a href="mysqlqb_functions.html#function_adddate"><code class="literal">ADDDATE()</code></a></td><td>Add dates</td></tr><tr><td><a href="mysqlqb_functions.html#function_addtime"><code class="literal">ADDTIME()</code></a></td><td>Add time</td></tr><tr><td><a href="mysqlqb_functions.html#function_aes-encrypt"><code class="literal">AES_DECRYPT()</code></a></td><td>Decrypt using AES</td></tr><tr><td><a href="mysqlqb_functions.html#function_aes-encrypt"><code class="literal">AES_ENCRYPT()</code></a></td><td>Encrypt using AES</td></tr><tr><td><a href="mysqlqb_functions.html#operator_and"><code class="literal">AND</code>, <code class="literal">&&</code></a></td><td>Logical AND</td></tr><tr><td><a href="mysqlqb_functions.html#function_ascii"><code class="literal">ASCII()</code></a></td><td>Return numeric value of left-most character</td></tr><tr><td><a href="mysqlqb_functions.html#function_asin"><code class="literal">ASIN()</code></a></td><td>Return the arc sine</td></tr><tr><td><a href="mysqlqb_functions.html#function_atan2"><code class="literal">ATAN2()</code>, <code class="literal">ATAN()</code></a></td><td>Return the arc tangent of the two arguments</td></tr><tr><td><a href="mysqlqb_functions.html#function_atan"><code class="literal">ATAN()</code></a></td><td>Return the arc tangent</td></tr><tr><td><a href="mysqlqb_functions.html#function_avg"><code class="literal">AVG()</code></a></td><td>Return the average value of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_benchmark"><code class="literal">BENCHMARK()</code></a></td><td>Repeatedly execute an expression</td></tr><tr><td><a href="mysqlqb_functions.html#function_between"><code class="literal">BETWEEN ... AND ... </code></a></td><td>Check whether a value is within a range of values</td></tr><tr><td><a href="mysqlqb_functions.html#function_bin"><code class="literal">BIN()</code></a></td><td>Return a string representation of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#operator_binary"><code class="literal">BINARY</code></a></td><td>Cast a string to a binary string</td></tr><tr><td><a href="mysqlqb_functions.html#function_bit-and"><code class="literal">BIT_AND()</code></a></td><td>Return bitwise and</td></tr><tr><td><a href="mysqlqb_functions.html#function_bit-count"><code class="literal">BIT_COUNT()</code></a></td><td>Return the number of bits that are set</td></tr><tr><td><a href="mysqlqb_functions.html#function_bit-length"><code class="literal">BIT_LENGTH()</code></a></td><td>Return length of argument in bits</td></tr><tr><td><a href="mysqlqb_functions.html#function_bit-or"><code class="literal">BIT_OR()</code></a></td><td>Return bitwise or</td></tr><tr><td><a href="mysqlqb_functions.html#function_bit-xor"><code class="literal">BIT_XOR()</code></a></td><td>Return bitwise xor</td></tr><tr><td><a href="mysqlqb_functions.html#operator_bitwise-and"><code class="literal">&</code></a></td><td>Bitwise AND</td></tr><tr><td><a href="mysqlqb_functions.html#operator_bitwise-or"><code class="literal">|</code></a></td><td>Bitwise OR</td></tr><tr><td><a href="mysqlqb_functions.html#operator_bitwise-xor"><code class="literal">^</code></a></td><td>Bitwise XOR</td></tr><tr><td><a href="mysqlqb_functions.html#operator_by"><code class="literal">/</code></a></td><td>Division operator</td></tr><tr><td><a href="mysqlqb_functions.html#function_case"><code class="literal">CASE</code></a></td><td>Case statement</td></tr><tr><td><a href="mysqlqb_functions.html#function_cast"><code class="literal">CAST()</code></a></td><td>Cast a value as a certain type</td></tr><tr><td><a href="mysqlqb_functions.html#function_ceiling"><code class="literal">CEILING()</code>, <code class="literal">CEIL()</code></a></td><td>Return the smallest integer value not less than the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_char-length"><code class="literal">CHAR_LENGTH()</code></a></td><td>Return number of characters in argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_char"><code class="literal">CHAR()</code></a></td><td>Return the character for each integer passed</td></tr><tr><td><a href="mysqlqb_functions.html#function_character-length"><code class="literal">CHARACTER_LENGTH()</code></a></td><td>A synonym for CHAR_LENGTH()</td></tr><tr><td><a href="mysqlqb_functions.html#function_charset"><code class="literal">CHARSET()</code></a></td><td>Return the character set of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_coalesce"><code class="literal">COALESCE()</code></a></td><td>Return the first non-NULL argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_coercibility"><code class="literal">COERCIBILITY()</code></a></td><td>Return the collation coercibility value of the string argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_collation"><code class="literal">COLLATION()</code></a></td><td>Return the collation of the string argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_compress"><code class="literal">COMPRESS()</code></a></td><td>Return result as a binary string</td></tr><tr><td><a href="mysqlqb_functions.html#function_concat-ws"><code class="literal">CONCAT_WS()</code></a></td><td>Return concatenate with separator</td></tr><tr><td><a href="mysqlqb_functions.html#function_concat"><code class="literal">CONCAT()</code></a></td><td>Return concatenated string</td></tr><tr><td><a href="mysqlqb_functions.html#function_connection-id"><code class="literal">CONNECTION_ID()</code></a></td><td>Return the connection ID (thread ID) for the connection</td></tr><tr><td><a href="mysqlqb_functions.html#function_conv"><code class="literal">CONV()</code></a></td><td>Convert numbers between different number bases</td></tr><tr><td><a href="mysqlqb_functions.html#function_convert-tz"><code class="literal">CONVERT_TZ()</code></a></td><td>Convert from one timezone to another</td></tr><tr><td><a href="mysqlqb_functions.html#function_cos"><code class="literal">COS()</code></a></td><td>Return the cosine</td></tr><tr><td><a href="mysqlqb_functions.html#function_cot"><code class="literal">COT()</code></a></td><td>Return the cotangent</td></tr><tr><td><a href="mysqlqb_functions.html#function_count-distinct"><code class="literal">COUNT(DISTINCT)</code></a></td><td>Return the count of a number of different values</td></tr><tr><td><a href="mysqlqb_functions.html#function_count"><code class="literal">COUNT()</code></a></td><td>Return a count of the number of rows returned</td></tr><tr><td><a href="mysqlqb_functions.html#function_crc32"><code class="literal">CRC32()</code></a></td><td>Compute a cyclic redundancy check value</td></tr><tr><td><a href="mysqlqb_functions.html#function_curdate"><code class="literal">CURDATE()</code></a></td><td>Return the current date</td></tr><tr><td><a href="mysqlqb_functions.html#function_current-date"><code class="literal">CURRENT_DATE()</code>, <code class="literal">CURRENT_DATE</code></a></td><td>Synonyms for CURDATE()</td></tr><tr><td><a href="mysqlqb_functions.html#function_current-time"><code class="literal">CURRENT_TIME()</code>, <code class="literal">CURRENT_TIME</code></a></td><td>Synonyms for CURTIME()</td></tr><tr><td><a href="mysqlqb_functions.html#function_current-timestamp"><code class="literal">CURRENT_TIMESTAMP()</code>, <code class="literal">CURRENT_TIMESTAMP</code></a></td><td>Synonyms for NOW()</td></tr><tr><td><a href="mysqlqb_functions.html#function_current-user"><code class="literal">CURRENT_USER()</code>, <code class="literal">CURRENT_USER</code></a></td><td>Return the username and hostname combination</td></tr><tr><td><a href="mysqlqb_functions.html#function_curtime"><code class="literal">CURTIME()</code></a></td><td>Return the current time</td></tr><tr><td><a href="mysqlqb_functions.html#function_database"><code class="literal">DATABASE()</code></a></td><td>Return the default (current) database name</td></tr><tr><td><a href="mysqlqb_functions.html#function_date-add"><code class="literal">DATE_ADD()</code></a></td><td>Add two dates</td></tr><tr><td><a href="mysqlqb_functions.html#function_date-format"><code class="literal">DATE_FORMAT()</code></a></td><td>Format date as specified</td></tr><tr><td><a href="mysqlqb_functions.html#function_date-sub"><code class="literal">DATE_SUB()</code></a></td><td>Subtract two dates</td></tr><tr><td><a href="mysqlqb_functions.html#function_date"><code class="literal">DATE()</code></a></td><td>Extract the date part of a date or datetime expression</td></tr><tr><td><a href="mysqlqb_functions.html#function_datediff"><code class="literal">DATEDIFF()</code></a></td><td>Subtract two dates</td></tr><tr><td><a href="mysqlqb_functions.html#function_day"><code class="literal">DAY()</code></a></td><td>Synonym for DAYOFMONTH()</td></tr><tr><td><a href="mysqlqb_functions.html#function_dayname"><code class="literal">DAYNAME()</code></a></td><td>Return the name of the weekday</td></tr><tr><td><a href="mysqlqb_functions.html#function_dayofmonth"><code class="literal">DAYOFMONTH()</code></a></td><td>Return the day of the month (1-31)</td></tr><tr><td><a href="mysqlqb_functions.html#function_dayofweek"><code class="literal">DAYOFWEEK()</code></a></td><td>Return the weekday index of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_dayofyear"><code class="literal">DAYOFYEAR()</code></a></td><td>Return the day of the year (1-366)</td></tr><tr><td><a href="mysqlqb_functions.html#function_decode"><code class="literal">DECODE()</code></a></td><td>Decodes a string encrypted using ENCODE()</td></tr><tr><td><a href="mysqlqb_functions.html#function_default"><code class="literal">DEFAULT()</code></a></td><td>Return the default value for a table column</td></tr><tr><td><a href="mysqlqb_functions.html#function_degrees"><code class="literal">DEGREES()</code></a></td><td>Convert radians to degrees</td></tr><tr><td><a href="mysqlqb_functions.html#function_des-decrypt"><code class="literal">DES_DECRYPT()</code></a></td><td>Decrypt a string</td></tr><tr><td><a href="mysqlqb_functions.html#function_des-encrypt"><code class="literal">DES_ENCRYPT()</code></a></td><td>Decrypt a string</td></tr><tr><td><a href="mysqlqb_functions.html#operator_div"><code class="literal">DIV</code></a></td><td>Integer division</td></tr><tr><td><a href="mysqlqb_functions.html#function_elt"><code class="literal">ELT()</code></a></td><td>Return string at index number</td></tr><tr><td><a href="mysqlqb_functions.html#function_encode"><code class="literal">ENCODE()</code></a></td><td>Encode a string</td></tr><tr><td><a href="mysqlqb_functions.html#function_encrypt"><code class="literal">ENCRYPT()</code></a></td><td>Encrypt a string</td></tr><tr><td><a href="mysqlqb_functions.html#operator_equal-to"><code class="literal"><=></code></a></td><td>NULL-safe equal to operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_equal"><code class="literal">=</code></a></td><td>Equal operator</td></tr><tr><td><a href="mysqlqb_functions.html#function_exp"><code class="literal">EXP()</code></a></td><td>Raise to the power of</td></tr><tr><td><a href="mysqlqb_functions.html#function_export-set"><code class="literal">EXPORT_SET()</code></a></td><td>Return a string such that for every bit set in the value bits, you get an on string and for every unset bit, you get an off string</td></tr><tr><td><a href="mysqlqb_functions.html#function_extract"><code class="literal">EXTRACT</code></a></td><td>Extract part of a date</td></tr><tr><td><a href="mysqlqb_functions.html#function_field"><code class="literal">FIELD()</code></a></td><td>Return the index (position) of the first argument in the subsequent arguments</td></tr><tr><td><a href="mysqlqb_functions.html#function_find-in-set"><code class="literal">FIND_IN_SET()</code></a></td><td>Return the number of times the first argument is found in the second arguments</td></tr><tr><td><a href="mysqlqb_functions.html#function_floor"><code class="literal">FLOOR()</code></a></td><td>Return the largest integer value not greater than the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_format"><code class="literal">FORMAT()</code></a></td><td>Return a number formatted to specified number of decimal places</td></tr><tr><td><a href="mysqlqb_functions.html#function_found-rows"><code class="literal">FOUND_ROWS()</code></a></td><td>For a SELECT with a LIMIT clause, the number of rows that would be returned were there no LIMIT clause</td></tr><tr><td><a href="mysqlqb_functions.html#function_from-days"><code class="literal">FROM_DAYS()</code></a></td><td>Convert a day number to a date</td></tr><tr><td><a href="mysqlqb_functions.html#function_from-unixtime"><code class="literal">FROM_UNIXTIME()</code></a></td><td>Format date as a UNIX timestamp</td></tr><tr><td><a href="mysqlqb_functions.html#function_get-format"><code class="literal">GET_FORMAT()</code></a></td><td>Return a date format string</td></tr><tr><td><a href="mysqlqb_functions.html#function_get-lock"><code class="literal">GET_LOCK()</code></a></td><td>Get a named lock</td></tr><tr><td><a href="mysqlqb_functions.html#operator_greater-than-or-equal"><code class="literal">>=</code></a></td><td>Greater than or equal operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_greater-than"><code class="literal">></code></a></td><td>Greater than operator</td></tr><tr><td><a href="mysqlqb_functions.html#function_greatest"><code class="literal">GREATEST()</code></a></td><td>Return the largest argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_group-concat"><code class="literal">GROUP_CONCAT()</code></a></td><td>Return a concatenated string</td></tr><tr><td><a href="mysqlqb_functions.html#function_hex"><code class="literal">HEX()</code></a></td><td>Return a string representation of a hex value</td></tr><tr><td><a href="mysqlqb_functions.html#function_hour"><code class="literal">HOUR()</code></a></td><td>Extract the hour</td></tr><tr><td><a href="mysqlqb_functions.html#function_if"><code class="literal">IF()</code></a></td><td>If/else construct</td></tr><tr><td><a href="mysqlqb_functions.html#function_ifnull"><code class="literal">IFNULL()</code></a></td><td>Null if/else construct</td></tr><tr><td><a href="mysqlqb_functions.html#function_in"><code class="literal">IN</code></a></td><td>Check whether a value is not within a set of values</td></tr><tr><td><a href="mysqlqb_functions.html#function_inet-aton"><code class="literal">INET_ATON()</code></a></td><td>Return the numeric value of an IP address</td></tr><tr><td><a href="mysqlqb_functions.html#function_inet-ntoa"><code class="literal">INET_NTOA()</code></a></td><td>Return the IP address from a numeric value</td></tr><tr><td><a href="mysqlqb_functions.html#function_insert"><code class="literal">INSERT()</code></a></td><td>Insert a substring at the specified position up to the specified number of characters</td></tr><tr><td><a href="mysqlqb_functions.html#function_instr"><code class="literal">INSTR()</code></a></td><td>Return the index of the first occurrence of substring</td></tr><tr><td><a href="mysqlqb_functions.html#function_interval"><code class="literal">INTERVAL()</code></a></td><td>Return the index of the argument that is less than the first argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_is-free-lock"><code class="literal">IS_FREE_LOCK()</code></a></td><td>Checks whether the named lock is free</td></tr><tr><td><a href="mysqlqb_functions.html#function_is-null"><code class="literal">IS NULL</code></a></td><td>NULL value test</td></tr><tr><td><a href="mysqlqb_functions.html#function_is-used-lock"><code class="literal">IS_USED_LOCK()</code></a></td><td>Checks whether the named lock is in use. Return connection identifier if true.</td></tr><tr><td><a href="mysqlqb_functions.html#operator_is"><code class="literal">IS</code></a></td><td>Test a value against a boolean</td></tr><tr><td><a href="mysqlqb_functions.html#function_isnull"><code class="literal">ISNULL()</code></a></td><td>Test whether the argument is NULL</td></tr><tr><td><a href="mysqlqb_functions.html#function_last-day"><code class="literal">LAST_DAY</code></a></td><td>Return the last day of the month for the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_last-insert-id"><code class="literal">LAST_INSERT_ID()</code></a></td><td>Value of the AUTOINCREMENT column for the last INSERT</td></tr><tr><td><a href="mysqlqb_functions.html#function_lcase"><code class="literal">LCASE()</code></a></td><td>Synonym for LOWER() </td></tr><tr><td><a href="mysqlqb_functions.html#function_least"><code class="literal">LEAST()</code></a></td><td>Return the smallest argument</td></tr><tr><td><a href="mysqlqb_functions.html#operator_left-shift"><code class="literal"><<</code></a></td><td>Left shift</td></tr><tr><td><a href="mysqlqb_functions.html#function_left"><code class="literal">LEFT()</code></a></td><td>Return the leftmost number of characters as specified</td></tr><tr><td><a href="mysqlqb_functions.html#function_length"><code class="literal">LENGTH()</code></a></td><td>Return the length of a string in bytes</td></tr><tr><td><a href="mysqlqb_functions.html#operator_less-than-or-equal"><code class="literal"><=</code></a></td><td>Less than or equal operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_less-than"><code class="literal"><</code></a></td><td>Less than operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_like"><code class="literal">LIKE</code></a></td><td>Simple pattern matching</td></tr><tr><td><a href="mysqlqb_functions.html#function_ln"><code class="literal">LN()</code></a></td><td>Return the natural logarithm of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_load-file"><code class="literal">LOAD_FILE()</code></a></td><td>Load the named file</td></tr><tr><td><a href="mysqlqb_functions.html#function_localtime"><code class="literal">LOCALTIME()</code>, <code class="literal">LOCALTIME</code></a></td><td>Synonym for NOW()</td></tr><tr><td><a href="mysqlqb_functions.html#function_localtimestamp"><code class="literal">LOCALTIMESTAMP</code>, <code class="literal">LOCALTIMESTAMP()</code></a></td><td>Synonym for NOW()</td></tr><tr><td><a href="mysqlqb_functions.html#function_locate"><code class="literal">LOCATE()</code></a></td><td>Return the position of the first occurrence of substring</td></tr><tr><td><a href="mysqlqb_functions.html#function_log10"><code class="literal">LOG10()</code></a></td><td>Return the base-10 logarithm of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_log2"><code class="literal">LOG2()</code></a></td><td>Return the base-2 logarithm of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_log"><code class="literal">LOG()</code></a></td><td> Return the natural logarithm of the first argument </td></tr><tr><td><a href="mysqlqb_functions.html#function_lower"><code class="literal">LOWER()</code></a></td><td>Return the argument in lowercase </td></tr><tr><td><a href="mysqlqb_functions.html#function_lpad"><code class="literal">LPAD()</code></a></td><td>Return the string argument, left-padded with the specified string</td></tr><tr><td><a href="mysqlqb_functions.html#function_ltrim"><code class="literal">LTRIM()</code></a></td><td>Remove leading spaces</td></tr><tr><td><a href="mysqlqb_functions.html#function_make-set"><code class="literal">MAKE_SET()</code></a></td><td>Return a set of comma-separated strings that have the corresponding bit in bits set</td></tr><tr><td><a href="mysqlqb_functions.html#function_makedate"><code class="literal">MAKEDATE()</code></a></td><td>Create a date from the year and day of year</td></tr><tr><td><a href="mysqlqb_functions.html#function_maketime"><code class="literal">MAKETIME</code></a></td><td>MAKETIME()</td></tr><tr><td><a href="mysqlqb_functions.html#function_master-pos-wait"><code class="literal">MASTER_POS_WAIT()</code></a></td><td>Block until the slave has read and applied all updates up to the specified position</td></tr><tr><td><a href="mysqlqb_functions.html#function_min"><code class="literal">MAX()</code></a></td><td>Return the maximum value</td></tr><tr><td><a href="mysqlqb_functions.html#function_md5"><code class="literal">MD5()</code></a></td><td>Calculate MD5 checksum</td></tr><tr><td><a href="mysqlqb_functions.html#function_microsecond"><code class="literal">MICROSECOND()</code></a></td><td>Return the microseconds from argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_mid"><code class="literal">MID()</code></a></td><td>Return a substring starting from the specified position</td></tr><tr><td><a href="mysqlqb_functions.html#function_min"><code class="literal">MIN()</code></a></td><td>Return the minimum value</td></tr><tr><td><a href="mysqlqb_functions.html#operator_minus"><code class="literal">-</code></a></td><td>Minus operator</td></tr><tr><td><a href="mysqlqb_functions.html#function_minute"><code class="literal">MINUTE()</code></a></td><td>Return the minute from the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_mod"><code class="literal">MOD()</code></a></td><td>Return the remainder</td></tr><tr><td><a href="mysqlqb_functions.html#operator_mod"><code class="literal">%</code></a></td><td>Modulo operator</td></tr><tr><td><a href="mysqlqb_functions.html#function_month"><code class="literal">MONTH()</code></a></td><td>Return the month from the date passed</td></tr><tr><td><a href="mysqlqb_functions.html#function_monthname"><code class="literal">MONTHNAME()</code></a></td><td>Return the name of the month</td></tr><tr><td><a href="mysqlqb_functions.html#function_name-const"><code class="literal">NAME_CONST()</code></a>(v5.0.12)</td><td>Causes the column to have the given name</td></tr><tr><td><a href="mysqlqb_functions.html#function_not-between"><code class="literal">NOT BETWEEN ... AND ...</code></a></td><td>Check whether a value is not within a range of values</td></tr><tr><td><a href="mysqlqb_functions.html#operator_not-equal"><code class="literal">!=</code>, <code class="literal"><></code></a></td><td>Not equal operator</td></tr><tr><td><a href="mysqlqb_functions.html#function_not-in"><code class="literal">NOT IN</code></a></td><td>Check whether a value is not within a set of values</td></tr><tr><td><a href="mysqlqb_functions.html#operator_not-like"><code class="literal">NOT LIKE</code></a></td><td>Negation of simple pattern matching</td></tr><tr><td><a href="mysqlqb_functions.html#operator_not-regexp"><code class="literal">NOT RGEXP</code></a></td><td>Negation of REGEXP</td></tr><tr><td><a href="mysqlqb_functions.html#operator_not"><code class="literal">NOT</code>, <code class="literal">!</code></a></td><td>Negates value</td></tr><tr><td><a href="mysqlqb_functions.html#function_now"><code class="literal">NOW()</code></a></td><td>Return the current date and time</td></tr><tr><td><a href="mysqlqb_functions.html#function_nullif"><code class="literal">NULLIF()</code></a></td><td>Return NULL if expr1 = expr2</td></tr><tr><td><a href="mysqlqb_functions.html#function_oct"><code class="literal">OCT()</code></a></td><td>Return a string representation of the octal argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_octet-length"><code class="literal">OCTET_LENGTH()</code></a></td><td>A synonym for LENGTH()</td></tr><tr><td><a href="mysqlqb_functions.html#function_old-password"><code class="literal">OLD_PASSWORD()</code></a></td><td>Return the value of the old (pre-4.1) implementation of PASSWORD</td></tr><tr><td><a href="mysqlqb_functions.html#operator_or"><code class="literal">||</code>, <code class="literal">OR</code></a></td><td>Logical OR</td></tr><tr><td><a href="mysqlqb_functions.html#function_ord"><code class="literal">ORD()</code></a></td><td>If the leftmost character of the argument is a multi-byte character, returns the code for that character</td></tr><tr><td><a href="mysqlqb_functions.html#function_password"><code class="literal">PASSWORD()</code></a></td><td>Calculate and return a password string</td></tr><tr><td><a href="mysqlqb_functions.html#function_period-add"><code class="literal">PERIOD_ADD()</code></a></td><td>Add a period to a year-month</td></tr><tr><td><a href="mysqlqb_functions.html#function_period-diff"><code class="literal">PERIOD_DIFF()</code></a></td><td>Return the number of months between periods</td></tr><tr><td><a href="mysqlqb_functions.html#function_pi"><code class="literal">PI()</code></a></td><td>Return the value of pi</td></tr><tr><td><a href="mysqlqb_functions.html#operator_plus"><code class="literal">+</code></a></td><td>Addition operator</td></tr><tr><td><a href="mysqlqb_functions.html#function_position"><code class="literal">POSITION()</code></a></td><td>A synonym for LOCATE()</td></tr><tr><td><a href="mysqlqb_functions.html#function_pow"><code class="literal">POW()</code>, <code class="literal">POWER()</code></a></td><td>Return the argument raised to the specified power</td></tr><tr><td><a href="mysqlqb_functions.html#function_quarter"><code class="literal">QUARTER()</code></a></td><td>Return the quarter from a date argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_quote"><code class="literal">QUOTE()</code></a></td><td>Escape the argument for use in an SQL statement</td></tr><tr><td><a href="mysqlqb_functions.html#function_radians"><code class="literal">RADIANS()</code></a></td><td>Return argument converted to radians</td></tr><tr><td><a href="mysqlqb_functions.html#function_rand"><code class="literal">RAND()</code></a></td><td>Return a random floating-point value</td></tr><tr><td><a href="mysqlqb_functions.html#operator_regexp"><code class="literal">REGEXP</code></a></td><td>Pattern matching using regular expressions</td></tr><tr><td><a href="mysqlqb_functions.html#function_release-lock"><code class="literal">RELEASE_LOCK()</code></a></td><td>Releases the named lock</td></tr><tr><td><a href="mysqlqb_functions.html#function_repeat"><code class="literal">REPEAT()</code></a></td><td>Repeat a string the specified number of times</td></tr><tr><td><a href="mysqlqb_functions.html#function_replace"><code class="literal">REPLACE()</code></a></td><td>Replace occurrences of a specified string</td></tr><tr><td><a href="mysqlqb_functions.html#function_reverse"><code class="literal">REVERSE()</code></a></td><td>Reverse the characters in a string</td></tr><tr><td><a href="mysqlqb_functions.html#operator_right-shift"><code class="literal">>></code></a></td><td>Right shift</td></tr><tr><td><a href="mysqlqb_functions.html#function_right"><code class="literal">RIGHT()</code></a></td><td>Return the specified rightmost number of characters</td></tr><tr><td><a href="mysqlqb_functions.html#operator_regexp"><code class="literal">RLIKE</code></a></td><td>Synonym for REGEXP</td></tr><tr><td><a href="mysqlqb_functions.html#function_round"><code class="literal">ROUND()</code></a></td><td>Round the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_row-count"><code class="literal">ROW_COUNT()</code></a></td><td>The number of rows updated</td></tr><tr><td><a href="mysqlqb_functions.html#function_rpad"><code class="literal">RPAD()</code></a></td><td>Append string the specified number of times</td></tr><tr><td><a href="mysqlqb_functions.html#function_rtrim"><code class="literal">RTRIM()</code></a></td><td>Remove trailing spaces</td></tr><tr><td><a href="mysqlqb_functions.html#function_schema"><code class="literal">SCHEMA()</code></a></td><td>A synonym for DATABASE()</td></tr><tr><td><a href="mysqlqb_functions.html#function_sec-to-time"><code class="literal">SEC_TO_TIME()</code></a></td><td>Converts seconds to 'HH:MM:SS' format</td></tr><tr><td><a href="mysqlqb_functions.html#function_second"><code class="literal">SECOND()</code></a></td><td>Return the second (0-59)</td></tr><tr><td><a href="mysqlqb_functions.html#function_session-user"><code class="literal">SESSION_USER()</code></a></td><td>Synonym for USER()</td></tr><tr><td><a href="mysqlqb_functions.html#function_sha1"><code class="literal">SHA1()</code>, <code class="literal">SHA()</code></a></td><td>Calculate an SHA-1 160-bit checksum</td></tr><tr><td><a href="mysqlqb_functions.html#function_sign"><code class="literal">SIGN()</code></a></td><td>Return the sign of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_sin"><code class="literal">SIN()</code></a></td><td>Return the sine of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_sleep"><code class="literal">SLEEP()</code></a></td><td>Sleep for a number of seconds</td></tr><tr><td><a href="mysqlqb_functions.html#function_soundex"><code class="literal">SOUNDEX()</code></a></td><td>Return a soundex string</td></tr><tr><td><a href="mysqlqb_functions.html#operator_sounds-like"><code class="literal">SOUNDS LIKE</code></a></td><td>Compare sounds</td></tr><tr><td><a href="mysqlqb_functions.html#function_space"><code class="literal">SPACE()</code></a></td><td>Return a string of the specified number of spaces</td></tr><tr><td><a href="mysqlqb_functions.html#function_sqrt"><code class="literal">SQRT()</code></a></td><td>Return the square root of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_std"><code class="literal">STD()</code>, <code class="literal">STDDEV()</code></a></td><td>Return the population standard deviation</td></tr><tr><td><a href="mysqlqb_functions.html#function_stddev-pop"><code class="literal">STDDEV_POP()</code></a></td><td>Return the population standard deviation</td></tr><tr><td><a href="mysqlqb_functions.html#function_stddev-samp"><code class="literal">STDDEV_SAMP()</code></a></td><td>Return the sample standard deviation</td></tr><tr><td><a href="mysqlqb_functions.html#function_str-to-date"><code class="literal">STR_TO_DATE()</code></a></td><td>Convert a string to a date</td></tr><tr><td><a href="mysqlqb_functions.html#function_strcmp"><code class="literal">STRCMP()</code></a></td><td>Compare two strings</td></tr><tr><td><a href="mysqlqb_functions.html#function_subdate"><code class="literal">SUBDATE()</code></a></td><td>When invoked with three arguments a synonym for DATE_SUB()</td></tr><tr><td><a href="mysqlqb_functions.html#function_substring-index"><code class="literal">SUBSTRING_INDEX()</code></a></td><td>Return a substring from a string before the specified number of occurrences of the delimiter</td></tr><tr><td><a href="mysqlqb_functions.html#function_substring"><code class="literal">SUBSTRING()</code></a></td><td>Return the substring as specified</td></tr><tr><td><a href="mysqlqb_functions.html#function_subtime"><code class="literal">SUBTIME()</code></a></td><td>Subtract times</td></tr><tr><td><a href="mysqlqb_functions.html#function_sum"><code class="literal">SUM()</code></a></td><td>Return the sum</td></tr><tr><td><a href="mysqlqb_functions.html#function_sysdate"><code class="literal">SYSDATE()</code></a></td><td>Return the time at which the function executes</td></tr><tr><td><a href="mysqlqb_functions.html#function_system-user"><code class="literal">SYSTEM_USER()</code></a></td><td>Synonym for USER()</td></tr><tr><td><a href="mysqlqb_functions.html#function_tan"><code class="literal">TAN()</code></a></td><td>Return the tangent of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#operator_tilde"><code class="literal">~</code></a></td><td>Invert bits</td></tr><tr><td><a href="mysqlqb_functions.html#function_time-format"><code class="literal">TIME_FORMAT()</code></a></td><td>Format as time</td></tr><tr><td><a href="mysqlqb_functions.html#function_time-to-sec"><code class="literal">TIME_TO_SEC()</code></a></td><td>Return the argument converted to seconds</td></tr><tr><td><a href="mysqlqb_functions.html#function_time"><code class="literal">TIME()</code></a></td><td>Extract the time portion of the expression passed</td></tr><tr><td><a href="mysqlqb_functions.html#function_timediff"><code class="literal">TIMEDIFF()</code></a></td><td>Subtract time</td></tr><tr><td><a href="mysqlqb_functions.html#operator_times"><code class="literal">*</code></a></td><td>Times operator</td></tr><tr><td><a href="mysqlqb_functions.html#function_timestamp"><code class="literal">TIMESTAMP()</code></a></td><td>With a single argument, this function returns the date or datetime expression. With two arguments, the sum of the arguments</td></tr><tr><td><a href="mysqlqb_functions.html#function_timestampadd"><code class="literal">TIMESTAMPADD()</code></a></td><td>Add an interval to a datetime expression</td></tr><tr><td><a href="mysqlqb_functions.html#function_timestampdiff"><code class="literal">TIMESTAMPDIFF()</code></a></td><td>Subtract an interval from a datetime expression</td></tr><tr><td><a href="mysqlqb_functions.html#function_to-days"><code class="literal">TO_DAYS()</code></a></td><td>Return the date argument converted to days</td></tr><tr><td><a href="mysqlqb_functions.html#function_trim"><code class="literal">TRIM()</code></a></td><td>Return </td></tr><tr><td><a href="mysqlqb_functions.html#function_truncate"><code class="literal">TRUNCATE()</code></a></td><td>Truncate to specified number of decimal places</td></tr><tr><td><a href="mysqlqb_functions.html#function_ucase"><code class="literal">UCASE()</code></a></td><td>Synonym for UPPER()</td></tr><tr><td><a href="mysqlqb_functions.html#operator_unary-minus"><code class="literal">-</code></a></td><td>Change the sign of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_uncompress"><code class="literal">UNCOMPRESS()</code></a></td><td>Uncompress a string compressed</td></tr><tr><td><a href="mysqlqb_functions.html#function_uncompressed-length"><code class="literal">UNCOMPRESSED_LENGTH()</code></a></td><td>Return the length of a string before compression</td></tr><tr><td><a href="mysqlqb_functions.html#function_unhex"><code class="literal">UNHEX()</code></a></td><td>Convert each pair of hexadecimal digits to a character</td></tr><tr><td><a href="mysqlqb_functions.html#function_unix-timestamp"><code class="literal">UNIX_TIMESTAMP()</code></a></td><td>Return a UNIX timestamp</td></tr><tr><td><a href="mysqlqb_functions.html#function_upper"><code class="literal">UPPER()</code></a></td><td>Convert to uppercase</td></tr><tr><td><a href="mysqlqb_functions.html#function_user"><code class="literal">USER()</code></a></td><td>Return the current username and hostname</td></tr><tr><td><a href="mysqlqb_functions.html#function_utc-date"><code class="literal">UTC_DATE()</code></a></td><td>Return the current UTC date</td></tr><tr><td><a href="mysqlqb_functions.html#function_utc-time"><code class="literal">UTC_TIME()</code></a></td><td>Return the current UTC time</td></tr><tr><td><a href="mysqlqb_functions.html#function_utc-timestamp"><code class="literal">UTC_TIMESTAMP()</code></a></td><td>Return the current UTC date and time</td></tr><tr><td><a href="mysqlqb_functions.html#function_uuid"><code class="literal">UUID()</code></a></td><td>Return a Universal Unique Identifier (UUID)</td></tr><tr><td><a href="mysqlqb_functions.html#function_values"><code class="literal">VALUES()</code></a></td><td>Defines the values to be used during an INSERT</td></tr><tr><td><a href="mysqlqb_functions.html#function_var-pop"><code class="literal">VAR_POP()</code></a></td><td>Return the population standard variance</td></tr><tr><td><a href="mysqlqb_functions.html#function_var-samp"><code class="literal">VAR_SAMP()</code></a></td><td>Return the sample variance</td></tr><tr><td><a href="mysqlqb_functions.html#function_variance"><code class="literal">VARIANCE()</code></a></td><td>Return the population standard variance</td></tr><tr><td><a href="mysqlqb_functions.html#function_week"><code class="literal">WEEK()</code></a></td><td>Return the week number</td></tr><tr><td><a href="mysqlqb_functions.html#function_weekday"><code class="literal">WEEKDAY()</code></a></td><td>Return the weekday index</td></tr><tr><td><a href="mysqlqb_functions.html#function_weekofyear"><code class="literal">WEEKOFYEAR()</code></a></td><td>Return the calendar week of the date (1-53)</td></tr><tr><td><a href="mysqlqb_functions.html#operator_xor"><code class="literal">XOR</code></a></td><td>Logical XOR</td></tr><tr><td><a href="mysqlqb_functions.html#function_year"><code class="literal">YEAR()</code></a></td><td>Return the year</td></tr><tr><td><a href="mysqlqb_functions.html#function_yearweek"><code class="literal">YEARWEEK()</code></a></td><td>Return the year and week</td></tr></tbody></table></div></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="non-typed-operators"></a>Operators</h2></div></div></div><div class="toc"><dl><dt><span class="section"><a href="mysqlqb_functions.html#operator-precedence">Operator Precedence</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#type-conversion">Type Conversion in Expression Evaluation</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#comparison-operators">Comparison Functions and Operators</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#logical-operators">Logical Operators</a></span></dt></dl></div><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><thead><tr><th><span class="bold"><strong>Name</strong></span></th><th><span class="bold"><strong>Description</strong></span></th></tr></thead><tbody><tr><td><a href="mysqlqb_functions.html#operator_and"><code class="literal">AND</code>, <code class="literal">&&</code></a></td><td>Logical AND</td></tr><tr><td><a href="mysqlqb_functions.html#operator_binary"><code class="literal">BINARY</code></a></td><td>Cast a string to a binary string</td></tr><tr><td><a href="mysqlqb_functions.html#operator_bitwise-and"><code class="literal">&</code></a></td><td>Bitwise AND</td></tr><tr><td><a href="mysqlqb_functions.html#operator_bitwise-or"><code class="literal">|</code></a></td><td>Bitwise OR</td></tr><tr><td><a href="mysqlqb_functions.html#operator_bitwise-xor"><code class="literal">^</code></a></td><td>Bitwise XOR</td></tr><tr><td><a href="mysqlqb_functions.html#operator_by"><code class="literal">/</code></a></td><td>Division operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_div"><code class="literal">DIV</code></a></td><td>Integer division</td></tr><tr><td><a href="mysqlqb_functions.html#operator_equal-to"><code class="literal"><=></code></a></td><td>NULL-safe equal to operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_equal"><code class="literal">=</code></a></td><td>Equal operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_greater-than-or-equal"><code class="literal">>=</code></a></td><td>Greater than or equal operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_greater-than"><code class="literal">></code></a></td><td>Greater than operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_is"><code class="literal">IS</code></a></td><td>Test a value against a boolean</td></tr><tr><td><a href="mysqlqb_functions.html#operator_left-shift"><code class="literal"><<</code></a></td><td>Left shift</td></tr><tr><td><a href="mysqlqb_functions.html#operator_less-than-or-equal"><code class="literal"><=</code></a></td><td>Less than or equal operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_less-than"><code class="literal"><</code></a></td><td>Less than operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_like"><code class="literal">LIKE</code></a></td><td>Simple pattern matching</td></tr><tr><td><a href="mysqlqb_functions.html#operator_minus"><code class="literal">-</code></a></td><td>Minus operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_mod"><code class="literal">%</code></a></td><td>Modulo operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_not-equal"><code class="literal">!=</code>, <code class="literal"><></code></a></td><td>Not equal operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_not-like"><code class="literal">NOT LIKE</code></a></td><td>Negation of simple pattern matching</td></tr><tr><td><a href="mysqlqb_functions.html#operator_not-regexp"><code class="literal">NOT RGEXP</code></a></td><td>Negation of REGEXP</td></tr><tr><td><a href="mysqlqb_functions.html#operator_not"><code class="literal">NOT</code>, <code class="literal">!</code></a></td><td>Negates value</td></tr><tr><td><a href="mysqlqb_functions.html#operator_or"><code class="literal">||</code>, <code class="literal">OR</code></a></td><td>Logical OR</td></tr><tr><td><a href="mysqlqb_functions.html#operator_plus"><code class="literal">+</code></a></td><td>Addition operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_regexp"><code class="literal">REGEXP</code></a></td><td>Pattern matching using regular expressions</td></tr><tr><td><a href="mysqlqb_functions.html#operator_right-shift"><code class="literal">>></code></a></td><td>Right shift</td></tr><tr><td><a href="mysqlqb_functions.html#operator_regexp"><code class="literal">RLIKE</code></a></td><td>Synonym for REGEXP</td></tr><tr><td><a href="mysqlqb_functions.html#operator_sounds-like"><code class="literal">SOUNDS LIKE</code></a></td><td>Compare sounds</td></tr><tr><td><a href="mysqlqb_functions.html#operator_tilde"><code class="literal">~</code></a></td><td>Invert bits</td></tr><tr><td><a href="mysqlqb_functions.html#operator_times"><code class="literal">*</code></a></td><td>Times operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_unary-minus"><code class="literal">-</code></a></td><td>Change the sign of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#operator_xor"><code class="literal">XOR</code></a></td><td>Logical XOR</td></tr></tbody></table></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="operator-precedence"></a>Operator Precedence</h3></div></div></div><a id="id2814446" class="indexterm"></a><a id="id2810577" class="indexterm"></a><p>
Operator precedences are shown in the following list, from
lowest precedence to the highest. Operators that are shown
together on a line have the same precedence.
</p><pre class="programlisting">:=
||, OR, XOR
&&, AND
NOT
BETWEEN, CASE, WHEN, THEN, ELSE
=, <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN
|
&
<<, >>
-, +
*, /, DIV, %, MOD
^
- (unary minus), ~ (unary bit inversion)
!
BINARY, COLLATE
</pre><p>
The precedence shown for <code class="literal">NOT</code> is as of MySQL
5.0.2. For earlier versions, or from 5.0.2 on if the
<code class="literal">HIGH_NOT_PRECEDENCE</code> SQL mode is enabled, the
precedence of <code class="literal">NOT</code> is the same as that of the
<code class="literal">!</code> operator. See
<a href="http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html" target="_top">SQL Modes</a>.
</p><a id="id2814750" class="indexterm"></a><a id="id2814757" class="indexterm"></a><a id="id2814763" class="indexterm"></a><a id="id2814773" class="indexterm"></a><p>
The precedence of operators determines the order of evaluation
of terms in an expression. To override this order and group
terms explicitly, use parentheses. For example:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 1+2*3;</code></strong>
-> 7
mysql> <strong class="userinput"><code>SELECT (1+2)*3;</code></strong>
-> 9
</pre></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="type-conversion"></a>Type Conversion in Expression Evaluation</h3></div></div></div><a id="id2814810" class="indexterm"></a><a id="id2814817" class="indexterm"></a><p>
When an operator is used with operands of different types, type
conversion occurs to make the operands compatible. Some
conversions occur implicitly. For example, MySQL automatically
converts numbers to strings as necessary, and vice versa.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 1+'1';</code></strong>
-> 2
mysql> <strong class="userinput"><code>SELECT CONCAT(2,' test');</code></strong>
-> '2 test'
</pre><p>
It is also possible to perform explicit conversions. If you want
to convert a number to a string explicitly, use the
<code class="literal">CAST()</code> or <code class="literal">CONCAT()</code>
function (<code class="literal">CAST()</code> is preferable):
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 38.8, CAST(38.8 AS CHAR);</code></strong>
-> 38.8, '38.8'
mysql> <strong class="userinput"><code>SELECT 38.8, CONCAT(38.8);</code></strong>
-> 38.8, '38.8'
</pre><p>
The following rules describe how conversion occurs for
comparison operations:
</p><div class="itemizedlist"><ul type="disc"><li><p>
If one or both arguments are <code class="literal">NULL</code>, the
result of the comparison is <code class="literal">NULL</code>, except
for the <code class="literal">NULL</code>-safe
<code class="literal"><=></code> equality comparison operator.
For <code class="literal">NULL <=> NULL</code>, the result is
true.
</p></li><li><p>
If both arguments in a comparison operation are strings,
they are compared as strings.
</p></li><li><p>
If both arguments are integers, they are compared as
integers.
</p></li><li><p>
Hexadecimal values are treated as binary strings if not
compared to a number.
</p></li><li><p>
<a id="id2814937" class="indexterm"></a>
<a id="id2814944" class="indexterm"></a>
If one of the arguments is a <code class="literal">TIMESTAMP</code> or
<code class="literal">DATETIME</code> column and the other argument is
a constant, the constant is converted to a timestamp before
the comparison is performed. This is done to be more
ODBC-friendly. Note that this is not done for the arguments
to <code class="literal">IN()</code>! To be safe, always use complete
datetime, date, or time strings when doing comparisons.
</p></li><li><p>
In all other cases, the arguments are compared as
floating-point (real) numbers.
</p></li></ul></div><p>
The following examples illustrate conversion of strings to
numbers for comparison operations:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 1 > '6x';</code></strong>
-> 0
mysql> <strong class="userinput"><code>SELECT 7 > '6x';</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT 0 > 'x6';</code></strong>
-> 0
mysql> <strong class="userinput"><code>SELECT 0 = 'x6';</code></strong>
-> 1
</pre><p>
Note that when you are comparing a string column with a number,
MySQL cannot use an index on the column to look up the value
quickly. If <em class="replaceable"><code>str_col</code></em> is an indexed
string column, the index cannot be used when performing the
lookup in the following statement:
</p><pre class="programlisting">SELECT * FROM <em class="replaceable"><code>tbl_name</code></em> WHERE <em class="replaceable"><code>str_col</code></em>=1;
</pre><p>
The reason for this is that there are many different strings
that may convert to the value <code class="literal">1</code>, such as
<code class="literal">'1'</code>, <code class="literal">' 1'</code>, or
<code class="literal">'1a'</code>.
</p><p>
Comparisons that use floating-point numbers (or values that are
converted to floating-point numbers) are approximate because
such numbers are inexact. This might lead to results that appear
inconsistent:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT '18015376320243458' = 18015376320243458;</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT '18015376320243459' = 18015376320243459;</code></strong>
-> 0
</pre><p>
Such results can occur because the values are converted to
floating-point numbers, which have only 53 bits of precision and
are subject to rounding:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT '18015376320243459'+0.0;</code></strong>
-> 1.8015376320243e+16
</pre><p>
Furthermore, the conversion from string to floating-point and
from integer to floating-point do not necessarily occur the same
way. The integer may be converted to floating-point by the CPU,
whereas the string is converted digit by digit in an operation
that involves floating-point multiplications.
</p><p>
The results shown will vary on different systems, and can be
affected by factors such as computer architecture or the
compiler version or optimization level. One way to avoid such
problems is to use <code class="literal">CAST()</code> so that a value
will not be converted implicitly to a float-point number:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT CAST('18015376320243459' AS UNSIGNED) = 18015376320243459;</code></strong>
-> 1
</pre><p>
For more information about floating-point comparisons, see
<a href="http://dev.mysql.com/doc/refman/5.0/en/problems-with-float.html" target="_top">Problems with Floating-Point Comparisons</a>.
</p></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="comparison-operators"></a>Comparison Functions and Operators</h3></div></div></div><a id="id2815139" class="indexterm"></a><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><thead><tr><th><span class="bold"><strong>Name</strong></span></th><th><span class="bold"><strong>Description</strong></span></th></tr></thead><tbody><tr><td><a href="mysqlqb_functions.html#function_between"><code class="literal">BETWEEN ... AND ... </code></a></td><td>Check whether a value is within a range of values</td></tr><tr><td><a href="mysqlqb_functions.html#function_coalesce"><code class="literal">COALESCE()</code></a></td><td>Return the first non-NULL argument</td></tr><tr><td><a href="mysqlqb_functions.html#operator_equal-to"><code class="literal"><=></code></a></td><td>NULL-safe equal to operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_equal"><code class="literal">=</code></a></td><td>Equal operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_greater-than-or-equal"><code class="literal">>=</code></a></td><td>Greater than or equal operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_greater-than"><code class="literal">></code></a></td><td>Greater than operator</td></tr><tr><td><a href="mysqlqb_functions.html#function_greatest"><code class="literal">GREATEST()</code></a></td><td>Return the largest argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_in"><code class="literal">IN</code></a></td><td>Check whether a value is not within a set of values</td></tr><tr><td><a href="mysqlqb_functions.html#function_interval"><code class="literal">INTERVAL()</code></a></td><td>Return the index of the argument that is less than the first argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_is-null"><code class="literal">IS NULL</code></a></td><td>NULL value test</td></tr><tr><td><a href="mysqlqb_functions.html#operator_is"><code class="literal">IS</code></a></td><td>Test a value against a boolean</td></tr><tr><td><a href="mysqlqb_functions.html#function_isnull"><code class="literal">ISNULL()</code></a></td><td>Test whether the argument is NULL</td></tr><tr><td><a href="mysqlqb_functions.html#function_least"><code class="literal">LEAST()</code></a></td><td>Return the smallest argument</td></tr><tr><td><a href="mysqlqb_functions.html#operator_less-than-or-equal"><code class="literal"><=</code></a></td><td>Less than or equal operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_less-than"><code class="literal"><</code></a></td><td>Less than operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_like"><code class="literal">LIKE</code></a></td><td>Simple pattern matching</td></tr><tr><td><a href="mysqlqb_functions.html#function_not-between"><code class="literal">NOT BETWEEN ... AND ...</code></a></td><td>Check whether a value is not within a range of values</td></tr><tr><td><a href="mysqlqb_functions.html#operator_not-equal"><code class="literal">!=</code>, <code class="literal"><></code></a></td><td>Not equal operator</td></tr><tr><td><a href="mysqlqb_functions.html#function_not-in"><code class="literal">NOT IN</code></a></td><td>Check whether a value is not within a set of values</td></tr><tr><td><a href="mysqlqb_functions.html#operator_not-like"><code class="literal">NOT LIKE</code></a></td><td>Negation of simple pattern matching</td></tr><tr><td><a href="mysqlqb_functions.html#operator_sounds-like"><code class="literal">SOUNDS LIKE</code></a></td><td>Compare sounds</td></tr></tbody></table></div><a id="id2815425" class="indexterm"></a><a id="id2815432" class="indexterm"></a><p>
Comparison operations result in a value of <code class="literal">1</code>
(<code class="literal">TRUE</code>), <code class="literal">0</code>
(<code class="literal">FALSE</code>), or <code class="literal">NULL</code>. These
operations work for both numbers and strings. Strings are
automatically converted to numbers and numbers to strings as
necessary.
</p><p>
Some of the functions in this section (such as
<code class="literal">LEAST()</code> and <code class="literal">GREATEST()</code>)
return values other than <code class="literal">1</code>
(<code class="literal">TRUE</code>), <code class="literal">0</code>
(<code class="literal">FALSE</code>), or <code class="literal">NULL</code>. However,
the value they return is based on comparison operations
performed according to the rules described in
<a href="mysqlqb_functions.html#type-conversion" title="Type Conversion in Expression Evaluation">Type Conversion in Expression Evaluation</a>.
</p><p>
To convert a value to a specific type for comparison purposes,
you can use the <code class="literal">CAST()</code> function. String
values can be converted to a different character set using
<code class="literal">CONVERT()</code>. See
<a href="mysqlqb_functions.html#cast-functions" title="Cast Functions and Operators">Cast Functions and Operators</a>.
</p><p>
By default, string comparisons are not case sensitive and use
the current character set. The default is
<code class="literal">latin1</code> (cp1252 West European), which also
works well for English.
</p><div class="itemizedlist"><ul type="disc"><li><p><a id="operator_equal"></a>
<a id="id2815550" class="indexterm"></a>
<a id="id2815557" class="indexterm"></a>
<code class="literal">=</code>
</p><p>
Equal:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 1 = 0;</code></strong>
-> 0
mysql> <strong class="userinput"><code>SELECT '0' = 0;</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT '0.0' = 0;</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT '0.01' = 0;</code></strong>
-> 0
mysql> <strong class="userinput"><code>SELECT '.01' = 0.01;</code></strong>
-> 1
</pre></li><li><p><a id="operator_equal-to"></a>
<a id="id2815625" class="indexterm"></a>
<a id="id2815636" class="indexterm"></a>
<code class="literal"><=></code>
</p><p>
<code class="literal">NULL</code>-safe equal. This operator performs
an equality comparison like the <code class="literal">=</code>
operator, but returns <code class="literal">1</code> rather than
<code class="literal">NULL</code> if both operands are
<code class="literal">NULL</code>, and <code class="literal">0</code> rather
than <code class="literal">NULL</code> if one operand is
<code class="literal">NULL</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 1 <=> 1, NULL <=> NULL, 1 <=> NULL;</code></strong>
-> 1, 1, 0
mysql> <strong class="userinput"><code>SELECT 1 = 1, NULL = NULL, 1 = NULL;</code></strong>
-> 1, NULL, NULL
</pre></li><li><p><a id="operator_not-equal"></a>
<a id="id2815740" class="indexterm"></a>
<a id="id2815747" class="indexterm"></a>
<a id="id2815754" class="indexterm"></a>
<a id="id2815761" class="indexterm"></a>
<code class="literal"><></code>, <code class="literal">!=</code>
</p><p>
Not equal:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT '.01' <> '0.01';</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT .01 <> '0.01';</code></strong>
-> 0
mysql> <strong class="userinput"><code>SELECT 'zapp' <> 'zappp';</code></strong>
-> 1
</pre></li><li><p><a id="operator_less-than-or-equal"></a>
<a id="id2815833" class="indexterm"></a>
<a id="id2815841" class="indexterm"></a>
<code class="literal"><=</code>
</p><p>
Less than or equal:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 0.1 <= 2;</code></strong>
-> 1
</pre></li><li><p><a id="operator_less-than"></a>
<a id="id2815901" class="indexterm"></a>
<a id="id2815908" class="indexterm"></a>
<code class="literal"><</code>
</p><p>
Less than:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 2 < 2;</code></strong>
-> 0
</pre></li><li><p><a id="operator_greater-than-or-equal"></a>
<a id="id2815969" class="indexterm"></a>
<a id="id2815976" class="indexterm"></a>
<code class="literal">>=</code>
</p><p>
Greater than or equal:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 2 >= 2;</code></strong>
-> 1
</pre></li><li><p><a id="operator_greater-than"></a>
<a id="id2816037" class="indexterm"></a>
<a id="id2816044" class="indexterm"></a>
<code class="literal">></code>
</p><p>
Greater than:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 2 > 2;</code></strong>
-> 0
</pre></li><li><p><a id="operator_is"></a>
<a id="id2816105" class="indexterm"></a>
<a id="id2816116" class="indexterm"></a>
<a id="id2816127" class="indexterm"></a>
<a id="id2816138" class="indexterm"></a>
<a id="id2816145" class="indexterm"></a>
<code class="literal">IS
<em class="replaceable"><code>boolean_value</code></em></code>,
<code class="literal">IS NOT
<em class="replaceable"><code>boolean_value</code></em></code>
</p><p>
Tests a value against a boolean value, where
<em class="replaceable"><code>boolean_value</code></em> can be
<code class="literal">TRUE</code>, <code class="literal">FALSE</code>, or
<code class="literal">UNKNOWN</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 1 IS TRUE, 0 IS FALSE, NULL IS UNKNOWN;</code></strong>
-> 1, 1, 1
mysql> <strong class="userinput"><code>SELECT 1 IS NOT UNKNOWN, 0 IS NOT UNKNOWN, NULL IS NOT UNKNOWN;</code></strong>
-> 1, 1, 0
</pre><p>
<code class="literal">IS [NOT]
<em class="replaceable"><code>boolean_value</code></em></code> syntax
was added in MySQL 5.0.2.
</p></li><li><p><a id="function_is-null"></a>
<a id="id2816254" class="indexterm"></a>
<a id="id2816265" class="indexterm"></a>
<a id="id2816272" class="indexterm"></a>
<code class="literal">IS NULL</code>, <code class="literal">IS NOT NULL</code>
</p><p>
Tests whether a value is or is not <code class="literal">NULL</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 1 IS NULL, 0 IS NULL, NULL IS NULL;</code></strong>
-> 0, 0, 1
mysql> <strong class="userinput"><code>SELECT 1 IS NOT NULL, 0 IS NOT NULL, NULL IS NOT NULL;</code></strong>
-> 1, 1, 0
</pre><p>
<a id="id2816329" class="indexterm"></a>
<a id="id2816335" class="indexterm"></a>
To work well with ODBC programs, MySQL supports the
following extra features when using <code class="literal">IS
NULL</code>:
</p><div class="itemizedlist"><ul type="circle"><li><p>
You can find the row that contains the most recent
<code class="literal">AUTO_INCREMENT</code> value by issuing a
statement of the following form immediately after
generating the value:
</p><pre class="programlisting">SELECT * FROM <em class="replaceable"><code>tbl_name</code></em> WHERE <em class="replaceable"><code>auto_col</code></em> IS NULL
</pre><p>
This behavior can be disabled by setting
<code class="literal">SQL_AUTO_IS_NULL=0</code>. See
<a href="mysqlqb_statements.html#set-option" title="SET Syntax"><code class="literal">SET</code> Syntax</a>.
</p></li><li><p>
For <code class="literal">DATE</code> and
<code class="literal">DATETIME</code> columns that are declared as
<code class="literal">NOT NULL</code>, you can find the special
date <code class="literal">'0000-00-00'</code> by using a
statement like this:
</p><pre class="programlisting">SELECT * FROM <em class="replaceable"><code>tbl_name</code></em> WHERE <em class="replaceable"><code>date_column</code></em> IS NULL
</pre><p>
This is needed to get some ODBC applications to work
because ODBC does not support a
<code class="literal">'0000-00-00'</code> date value.
</p></li></ul></div></li><li><p><a id="function_between"></a>
<a id="id2816465" class="indexterm"></a>
<code class="literal"><em class="replaceable"><code>expr</code></em> BETWEEN
<em class="replaceable"><code>min</code></em> AND
<em class="replaceable"><code>max</code></em></code>
</p><p>
If <em class="replaceable"><code>expr</code></em> is greater than or equal
to <em class="replaceable"><code>min</code></em> and
<em class="replaceable"><code>expr</code></em> is less than or equal to
<em class="replaceable"><code>max</code></em>, <code class="literal">BETWEEN</code>
returns <code class="literal">1</code>, otherwise it returns
<code class="literal">0</code>. This is equivalent to the expression
<code class="literal">(<em class="replaceable"><code>min</code></em> <=
<em class="replaceable"><code>expr</code></em> AND
<em class="replaceable"><code>expr</code></em> <=
<em class="replaceable"><code>max</code></em>)</code> if all the
arguments are of the same type. Otherwise type conversion
takes place according to the rules described in
<a href="mysqlqb_functions.html#type-conversion" title="Type Conversion in Expression Evaluation">Type Conversion in Expression Evaluation</a>, but applied to all the
three arguments.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 1 BETWEEN 2 AND 3;</code></strong>
-> 0
mysql> <strong class="userinput"><code>SELECT 'b' BETWEEN 'a' AND 'c';</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT 2 BETWEEN 2 AND '3';</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT 2 BETWEEN 2 AND 'x-3';</code></strong>
-> 0
</pre><p>
For best results when using <code class="literal">BETWEEN</code> with
date or time values, you should use
<code class="literal">CAST()</code> to explicitly convert the values
to the desired data type. Examples: If you compare a
<code class="literal">DATETIME</code> to two <code class="literal">DATE</code>
values, convert the <code class="literal">DATE</code> values to
<code class="literal">DATETIME</code> values. If you use a string
constant such as <code class="literal">'2001-1-1'</code> in a
comparison to a <code class="literal">DATE</code>, cast the string to
a <code class="literal">DATE</code>.
</p></li><li><p><a id="function_not-between"></a>
<a id="id2816644" class="indexterm"></a>
<code class="literal"><em class="replaceable"><code>expr</code></em> NOT BETWEEN
<em class="replaceable"><code>min</code></em> AND
<em class="replaceable"><code>max</code></em></code>
</p><p>
This is the same as <code class="literal">NOT
(<em class="replaceable"><code>expr</code></em> BETWEEN
<em class="replaceable"><code>min</code></em> AND
<em class="replaceable"><code>max</code></em>)</code>.
</p></li><li><p><a id="function_coalesce"></a>
<a id="id2816715" class="indexterm"></a>
<a id="id2816726" class="indexterm"></a>
<code class="literal">COALESCE(<em class="replaceable"><code>value</code></em>,...)</code>
</p><p>
Returns the first non-<code class="literal">NULL</code> value in the
list, or <code class="literal">NULL</code> if there are no
non-<code class="literal">NULL</code> values.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT COALESCE(NULL,1);</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT COALESCE(NULL,NULL,NULL);</code></strong>
-> NULL
</pre></li><li><p><a id="function_greatest"></a>
<a id="id2816807" class="indexterm"></a>
<code class="literal">GREATEST(<em class="replaceable"><code>value1</code></em>,<em class="replaceable"><code>value2</code></em>,...)</code>
</p><p>
With two or more arguments, returns the largest
(maximum-valued) argument. The arguments are compared using
the same rules as for <code class="literal">LEAST()</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT GREATEST(2,0);</code></strong>
-> 2
mysql> <strong class="userinput"><code>SELECT GREATEST(34.0,3.0,5.0,767.0);</code></strong>
-> 767.0
mysql> <strong class="userinput"><code>SELECT GREATEST('B','A','C');</code></strong>
-> 'C'
</pre><p>
Before MySQL 5.0.13, <code class="literal">GREATEST()</code> returns
<code class="literal">NULL</code> only if all arguments are
<code class="literal">NULL</code>. As of 5.0.13, it returns
<code class="literal">NULL</code> if any argument is
<code class="literal">NULL</code>.
</p></li><li><p><a id="function_in"></a>
<a id="id2816914" class="indexterm"></a>
<code class="literal"><em class="replaceable"><code>expr</code></em> IN
(<em class="replaceable"><code>value</code></em>,...)</code>
</p><p>
Returns <code class="literal">1</code> if
<em class="replaceable"><code>expr</code></em> is equal to any of the
values in the <code class="literal">IN</code> list, else returns
<code class="literal">0</code>. If all values are constants, they are
evaluated according to the type of
<em class="replaceable"><code>expr</code></em> and sorted. The search for
the item then is done using a binary search. This means
<code class="literal">IN</code> is very quick if the
<code class="literal">IN</code> value list consists entirely of
constants. Otherwise, type conversion takes place according
to the rules described in <a href="mysqlqb_functions.html#type-conversion" title="Type Conversion in Expression Evaluation">Type Conversion in Expression Evaluation</a>,
but applied to all the arguments.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 2 IN (0,3,5,7);</code></strong>
-> 0
mysql> <strong class="userinput"><code>SELECT 'wefwf' IN ('wee','wefwf','weg');</code></strong>
-> 1
</pre><p>
You should never mix quoted and unquoted values in an
<code class="literal">IN</code> list because the comparison rules for
quoted values (such as strings) and unquoted values (such as
numbers) differ. Mixing types may therefore lead to
inconsistent results. For example, do not write an
<code class="literal">IN</code> expression like this:
</p><pre class="programlisting">SELECT val1 FROM tbl1 WHERE val1 IN (1,2,'a');
</pre><p>
Instead, write it like this:
</p><pre class="programlisting">SELECT val1 FROM tbl1 WHERE val1 IN ('1','2','a');
</pre><p>
The number of values in the <code class="literal">IN</code> list is
only limited by the <code class="literal">max_allowed_packet</code>
value.
</p><p>
To comply with the SQL standard, <code class="literal">IN</code>
returns <code class="literal">NULL</code> not only if the expression
on the left hand side is <code class="literal">NULL</code>, but also
if no match is found in the list and one of the expressions
in the list is <code class="literal">NULL</code>.
</p><p>
<code class="literal">IN()</code> syntax can also be used to write
certain types of subqueries. See
<a href="mysqlqb_statements.html#any-in-some-subqueries" title="Subqueries with ANY, IN, and SOME">Subqueries with <code class="literal">ANY</code>, <code class="literal">IN</code>, and
<code class="literal">SOME</code></a>.
</p></li><li><p><a id="function_not-in"></a>
<a id="id2817098" class="indexterm"></a>
<code class="literal"><em class="replaceable"><code>expr</code></em> NOT IN
(<em class="replaceable"><code>value</code></em>,...)</code>
</p><p>
This is the same as <code class="literal">NOT
(<em class="replaceable"><code>expr</code></em> IN
(<em class="replaceable"><code>value</code></em>,...))</code>.
</p></li><li><p><a id="function_isnull"></a>
<a id="id2817164" class="indexterm"></a>
<code class="literal">ISNULL(<em class="replaceable"><code>expr</code></em>)</code>
</p><p>
If <em class="replaceable"><code>expr</code></em> is
<code class="literal">NULL</code>, <code class="literal">ISNULL()</code> returns
<code class="literal">1</code>, otherwise it returns
<code class="literal">0</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT ISNULL(1+1);</code></strong>
-> 0
mysql> <strong class="userinput"><code>SELECT ISNULL(1/0);</code></strong>
-> 1
</pre><p>
<code class="literal">ISNULL()</code> can be used instead of
<code class="literal">=</code> to test whether a value is
<code class="literal">NULL</code>. (Comparing a value to
<code class="literal">NULL</code> using <code class="literal">=</code> always
yields false.)
</p><p>
The <code class="literal">ISNULL()</code> function shares some special
behaviors with the <code class="literal">IS NULL</code> comparison
operator. See the description of <code class="literal">IS NULL</code>.
</p></li><li><p><a id="function_interval"></a>
<a id="id2817291" class="indexterm"></a>
<code class="literal">INTERVAL(<em class="replaceable"><code>N</code></em>,<em class="replaceable"><code>N1</code></em>,<em class="replaceable"><code>N2</code></em>,<em class="replaceable"><code>N3</code></em>,...)</code>
</p><p>
Returns <code class="literal">0</code> if <em class="replaceable"><code>N</code></em>
< <em class="replaceable"><code>N1</code></em>, <code class="literal">1</code> if
<em class="replaceable"><code>N</code></em> <
<em class="replaceable"><code>N2</code></em> and so on or
<code class="literal">-1</code> if <em class="replaceable"><code>N</code></em> is
<code class="literal">NULL</code>. All arguments are treated as
integers. It is required that <em class="replaceable"><code>N1</code></em>
< <em class="replaceable"><code>N2</code></em> <
<em class="replaceable"><code>N3</code></em> < <code class="literal">...</code>
< <em class="replaceable"><code>Nn</code></em> for this function to work
correctly. This is because a binary search is used (very
fast).
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT INTERVAL(23, 1, 15, 17, 30, 44, 200);</code></strong>
-> 3
mysql> <strong class="userinput"><code>SELECT INTERVAL(10, 1, 10, 100, 1000);</code></strong>
-> 2
mysql> <strong class="userinput"><code>SELECT INTERVAL(22, 23, 30, 44, 200);</code></strong>
-> 0
</pre></li><li><p><a id="function_least"></a>
<a id="id2817428" class="indexterm"></a>
<code class="literal">LEAST(<em class="replaceable"><code>value1</code></em>,<em class="replaceable"><code>value2</code></em>,...)</code>
</p><p>
With two or more arguments, returns the smallest
(minimum-valued) argument. The arguments are compared using
the following rules:
</p><div class="itemizedlist"><ul type="circle"><li><p>
If the return value is used in an
<code class="literal">INTEGER</code> context or all arguments are
integer-valued, they are compared as integers.
</p></li><li><p>
If the return value is used in a <code class="literal">REAL</code>
context or all arguments are real-valued, they are
compared as reals.
</p></li><li><p>
If any argument is a case-sensitive string, the
arguments are compared as case-sensitive strings.
</p></li><li><p>
In all other cases, the arguments are compared as
case-insensitive strings.
</p></li></ul></div><p>
Before MySQL 5.0.13, <code class="literal">LEAST()</code> returns
<code class="literal">NULL</code> only if all arguments are
<code class="literal">NULL</code>. As of 5.0.13, it returns
<code class="literal">NULL</code> if any argument is
<code class="literal">NULL</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT LEAST(2,0);</code></strong>
-> 0
mysql> <strong class="userinput"><code>SELECT LEAST(34.0,3.0,5.0,767.0);</code></strong>
-> 3.0
mysql> <strong class="userinput"><code>SELECT LEAST('B','A','C');</code></strong>
-> 'A'
</pre><p>
Note that the preceding conversion rules can produce strange
results in some borderline cases:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT CAST(LEAST(3600, 9223372036854775808.0) as SIGNED);</code></strong>
-> -9223372036854775808
</pre><p>
This happens because MySQL reads
<code class="literal">9223372036854775808.0</code> in an integer
context. The integer representation is not good enough to
hold the value, so it wraps to a signed integer.
</p></li></ul></div></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="logical-operators"></a>Logical Operators</h3></div></div></div><a id="id2817596" class="indexterm"></a><a id="id2817603" class="indexterm"></a><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><thead><tr><th><span class="bold"><strong>Name</strong></span></th><th><span class="bold"><strong>Description</strong></span></th></tr></thead><tbody><tr><td><a href="mysqlqb_functions.html#operator_and"><code class="literal">AND</code>, <code class="literal">&&</code></a></td><td>Logical AND</td></tr><tr><td><a href="mysqlqb_functions.html#operator_not"><code class="literal">NOT</code>, <code class="literal">!</code></a></td><td>Negates value</td></tr><tr><td><a href="mysqlqb_functions.html#operator_or"><code class="literal">||</code>, <code class="literal">OR</code></a></td><td>Logical OR</td></tr><tr><td><a href="mysqlqb_functions.html#operator_xor"><code class="literal">XOR</code></a></td><td>Logical XOR</td></tr></tbody></table></div><p>
In SQL, all logical operators evaluate to
<code class="literal">TRUE</code>, <code class="literal">FALSE</code>, or
<code class="literal">NULL</code> (<code class="literal">UNKNOWN</code>). In MySQL,
these are implemented as 1 (<code class="literal">TRUE</code>), 0
(<code class="literal">FALSE</code>), and <code class="literal">NULL</code>. Most of
this is common to different SQL database servers, although some
servers may return any non-zero value for
<code class="literal">TRUE</code>.
</p><div class="itemizedlist"><ul type="disc"><li><p><a id="operator_not"></a>
<a id="id2817773" class="indexterm"></a>
<a id="id2817783" class="indexterm"></a>
<code class="literal">NOT</code>, <code class="literal">!</code>
</p><p>
Logical NOT. Evaluates to <code class="literal">1</code> if the
operand is <code class="literal">0</code>, to <code class="literal">0</code> if
the operand is non-zero, and <code class="literal">NOT NULL</code>
returns <code class="literal">NULL</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT NOT 10;</code></strong>
-> 0
mysql> <strong class="userinput"><code>SELECT NOT 0;</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT NOT NULL;</code></strong>
-> NULL
mysql> <strong class="userinput"><code>SELECT ! (1+1);</code></strong>
-> 0
mysql> <strong class="userinput"><code>SELECT ! 1+1;</code></strong>
-> 1
</pre><p>
The last example produces <code class="literal">1</code> because the
expression evaluates the same way as
<code class="literal">(!1)+1</code>.
</p><p>
Note that the precedence of the <code class="literal">NOT</code>
operator changed in MySQL 5.0.2. See
<a href="mysqlqb_functions.html#operator-precedence" title="Operator Precedence">Operator Precedence</a>.
</p></li><li><p><a id="operator_and"></a>
<a id="id2817914" class="indexterm"></a>
<a id="id2817924" class="indexterm"></a>
<code class="literal">AND</code>, <code class="literal">&&</code>
</p><p>
Logical AND. Evaluates to <code class="literal">1</code> if all
operands are non-zero and not <code class="literal">NULL</code>, to
<code class="literal">0</code> if one or more operands are
<code class="literal">0</code>, otherwise <code class="literal">NULL</code> is
returned.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 1 && 1;</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT 1 && 0;</code></strong>
-> 0
mysql> <strong class="userinput"><code>SELECT 1 && NULL;</code></strong>
-> NULL
mysql> <strong class="userinput"><code>SELECT 0 && NULL;</code></strong>
-> 0
mysql> <strong class="userinput"><code>SELECT NULL && 0;</code></strong>
-> 0
</pre></li><li><p><a id="operator_or"></a>
<a id="id2818031" class="indexterm"></a>
<a id="id2818041" class="indexterm"></a>
<code class="literal">OR</code>, <code class="literal">||</code>
</p><p>
Logical OR. When both operands are
non-<code class="literal">NULL</code>, the result is
<code class="literal">1</code> if any operand is non-zero, and
<code class="literal">0</code> otherwise. With a
<code class="literal">NULL</code> operand, the result is
<code class="literal">1</code> if the other operand is non-zero, and
<code class="literal">NULL</code> otherwise. If both operands are
<code class="literal">NULL</code>, the result is
<code class="literal">NULL</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 1 || 1;</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT 1 || 0;</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT 0 || 0;</code></strong>
-> 0
mysql> <strong class="userinput"><code>SELECT 0 || NULL;</code></strong>
-> NULL
mysql> <strong class="userinput"><code>SELECT 1 || NULL;</code></strong>
-> 1
</pre></li><li><p><a id="operator_xor"></a>
<a id="id2818154" class="indexterm"></a>
<code class="literal">XOR</code>
</p><p>
Logical XOR. Returns <code class="literal">NULL</code> if either
operand is <code class="literal">NULL</code>. For
non-<code class="literal">NULL</code> operands, evaluates to
<code class="literal">1</code> if an odd number of operands is
non-zero, otherwise <code class="literal">0</code> is returned.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 1 XOR 1;</code></strong>
-> 0
mysql> <strong class="userinput"><code>SELECT 1 XOR 0;</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT 1 XOR NULL;</code></strong>
-> NULL
mysql> <strong class="userinput"><code>SELECT 1 XOR 1 XOR 1;</code></strong>
-> 1
</pre><p>
<code class="literal">a XOR b</code> is mathematically equal to
<code class="literal">(a AND (NOT b)) OR ((NOT a) and b)</code>.
</p></li></ul></div></div></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="control-flow-functions"></a>Control Flow Functions</h2></div></div></div><a id="id2818256" class="indexterm"></a><a id="id2818263" class="indexterm"></a><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><thead><tr><th><span class="bold"><strong>Name</strong></span></th><th><span class="bold"><strong>Description</strong></span></th></tr></thead><tbody><tr><td><a href="mysqlqb_functions.html#function_case"><code class="literal">CASE</code></a></td><td>Case statement</td></tr><tr><td><a href="mysqlqb_functions.html#function_if"><code class="literal">IF()</code></a></td><td>If/else construct</td></tr><tr><td><a href="mysqlqb_functions.html#function_ifnull"><code class="literal">IFNULL()</code></a></td><td>Null if/else construct</td></tr><tr><td><a href="mysqlqb_functions.html#function_nullif"><code class="literal">NULLIF()</code></a></td><td>Return NULL if expr1 = expr2</td></tr></tbody></table></div><div class="itemizedlist"><ul type="disc"><li><p><a id="function_case"></a>
<a id="id2818390" class="indexterm"></a>
<code class="literal">CASE <em class="replaceable"><code>value</code></em> WHEN
[<em class="replaceable"><code>compare_value</code></em>] THEN
<em class="replaceable"><code>result</code></em> [WHEN
[<em class="replaceable"><code>compare_value</code></em>] THEN
<em class="replaceable"><code>result</code></em> ...] [ELSE
<em class="replaceable"><code>result</code></em>] END</code>
</p><p>
<code class="literal">CASE WHEN [<em class="replaceable"><code>condition</code></em>] THEN
<em class="replaceable"><code>result</code></em> [WHEN
[<em class="replaceable"><code>condition</code></em>] THEN
<em class="replaceable"><code>result</code></em> ...] [ELSE
<em class="replaceable"><code>result</code></em>] END</code>
</p><p>
The first version returns the
<em class="replaceable"><code>result</code></em> where
<code class="literal"><em class="replaceable"><code>value</code></em>=<em class="replaceable"><code>compare_value</code></em></code>.
The second version returns the result for the first condition
that is true. If there was no matching result value, the
result after <code class="literal">ELSE</code> is returned, or
<code class="literal">NULL</code> if there is no <code class="literal">ELSE</code>
part.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT CASE 1 WHEN 1 THEN 'one'</code></strong>
-> <strong class="userinput"><code>WHEN 2 THEN 'two' ELSE 'more' END;</code></strong>
-> 'one'
mysql> <strong class="userinput"><code>SELECT CASE WHEN 1>0 THEN 'true' ELSE 'false' END;</code></strong>
-> 'true'
mysql> <strong class="userinput"><code>SELECT CASE BINARY 'B'</code></strong>
-> <strong class="userinput"><code>WHEN 'a' THEN 1 WHEN 'b' THEN 2 END;</code></strong>
-> NULL
</pre><p>
The default return type of a <code class="literal">CASE</code>
expression is the compatible aggregated type of all return
values, but also depends on the context in which it is used.
If used in a string context, the result is returned as a
string. If used in a numeric context, then the result is
returned as a decimal, real, or integer value.
</p><p>
<span class="bold"><strong>Note</strong></span>: The syntax of the
<code class="literal">CASE</code> <span class="emphasis"><em>expression</em></span> shown
here differs slightly from that of the SQL
<code class="literal">CASE</code> <span class="emphasis"><em>statement</em></span>
described in <a href="http://dev.mysql.com/doc/refman/5.0/en/case-statement.html" target="_top"><code class="literal">CASE</code> Statement</a>, for use inside
stored routines. The <code class="literal">CASE</code> statement cannot
have an <code class="literal">ELSE NULL</code> clause, and it is
terminated with <code class="literal">END CASE</code> instead of
<code class="literal">END</code>.
</p></li><li><p><a id="function_if"></a>
<a id="id2818618" class="indexterm"></a>
<code class="literal">IF(<em class="replaceable"><code>expr1</code></em>,<em class="replaceable"><code>expr2</code></em>,<em class="replaceable"><code>expr3</code></em>)</code>
</p><p>
If <em class="replaceable"><code>expr1</code></em> is <code class="literal">TRUE</code>
(<code class="literal"><em class="replaceable"><code>expr1</code></em> <>
0</code> and <code class="literal"><em class="replaceable"><code>expr1</code></em>
<> NULL</code>) then <code class="literal">IF()</code> returns
<em class="replaceable"><code>expr2</code></em>; otherwise it returns
<em class="replaceable"><code>expr3</code></em>. <code class="literal">IF()</code>
returns a numeric or string value, depending on the context in
which it is used.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT IF(1>2,2,3);</code></strong>
-> 3
mysql> <strong class="userinput"><code>SELECT IF(1<2,'yes','no');</code></strong>
-> 'yes'
mysql> <strong class="userinput"><code>SELECT IF(STRCMP('test','test1'),'no','yes');</code></strong>
-> 'no'
</pre><p>
If only one of <em class="replaceable"><code>expr2</code></em> or
<em class="replaceable"><code>expr3</code></em> is explicitly
<code class="literal">NULL</code>, the result type of the
<code class="literal">IF()</code> function is the type of the
non-<code class="literal">NULL</code> expression.
</p><p>
<em class="replaceable"><code>expr1</code></em> is evaluated as an integer
value, which means that if you are testing floating-point or
string values, you should do so using a comparison operation.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT IF(0.1,1,0);</code></strong>
-> 0
mysql> <strong class="userinput"><code>SELECT IF(0.1<>0,1,0);</code></strong>
-> 1
</pre><p>
In the first case shown, <code class="literal">IF(0.1)</code> returns
<code class="literal">0</code> because <code class="literal">0.1</code> is
converted to an integer value, resulting in a test of
<code class="literal">IF(0)</code>. This may not be what you expect. In
the second case, the comparison tests the original
floating-point value to see whether it is non-zero. The result
of the comparison is used as an integer.
</p><p>
The default return type of <code class="literal">IF()</code> (which may
matter when it is stored into a temporary table) is calculated
as follows:
</p><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><tbody><tr><td><span class="bold"><strong>Expression</strong></span></td><td><span class="bold"><strong>Return Value</strong></span></td></tr><tr><td><em class="replaceable"><code>expr2</code></em> or <em class="replaceable"><code>expr3</code></em>
returns a string</td><td>string</td></tr><tr><td><em class="replaceable"><code>expr2</code></em> or <em class="replaceable"><code>expr3</code></em>
returns a floating-point value</td><td>floating-point</td></tr><tr><td><em class="replaceable"><code>expr2</code></em> or <em class="replaceable"><code>expr3</code></em>
returns an integer</td><td>integer</td></tr></tbody></table></div><p>
If <em class="replaceable"><code>expr2</code></em> and
<em class="replaceable"><code>expr3</code></em> are both strings, the result
is case sensitive if either string is case sensitive.
</p><p>
<span class="bold"><strong>Note</strong></span>: There is also an
<code class="literal">IF</code> <span class="emphasis"><em>statement</em></span>, which
differs from the <code class="literal">IF()</code>
<span class="emphasis"><em>function</em></span> described here. See
<a href="http://dev.mysql.com/doc/refman/5.0/en/if-statement.html" target="_top"><code class="literal">IF</code> Statement</a>.
</p></li><li><p><a id="function_ifnull"></a>
<a id="id2818949" class="indexterm"></a>
<a id="id2818960" class="indexterm"></a>
<code class="literal">IFNULL(<em class="replaceable"><code>expr1</code></em>,<em class="replaceable"><code>expr2</code></em>)</code>
</p><p>
If <em class="replaceable"><code>expr1</code></em> is not
<code class="literal">NULL</code>, <code class="literal">IFNULL()</code> returns
<em class="replaceable"><code>expr1</code></em>; otherwise it returns
<em class="replaceable"><code>expr2</code></em>. <code class="literal">IFNULL()</code>
returns a numeric or string value, depending on the context in
which it is used.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT IFNULL(1,0);</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT IFNULL(NULL,10);</code></strong>
-> 10
mysql> <strong class="userinput"><code>SELECT IFNULL(1/0,10);</code></strong>
-> 10
mysql> <strong class="userinput"><code>SELECT IFNULL(1/0,'yes');</code></strong>
-> 'yes'
</pre><p>
The default result value of
<code class="literal">IFNULL(<em class="replaceable"><code>expr1</code></em>,<em class="replaceable"><code>expr2</code></em>)</code>
is the more “<span class="quote">general</span>” of the two expressions, in
the order <code class="literal">STRING</code>, <code class="literal">REAL</code>,
or <code class="literal">INTEGER</code>. Consider the case of a table
based on expressions or where MySQL must internally store a
value returned by <code class="literal">IFNULL()</code> in a temporary
table:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>CREATE TABLE tmp SELECT IFNULL(1,'test') AS test;</code></strong>
mysql> <strong class="userinput"><code>DESCRIBE tmp;</code></strong>
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| test | char(4) | | | | |
+-------+---------+------+-----+---------+-------+
</pre><p>
In this example, the type of the <code class="literal">test</code>
column is <code class="literal">CHAR(4)</code>.
</p></li><li><p><a id="function_nullif"></a>
<a id="id2819129" class="indexterm"></a>
<code class="literal">NULLIF(<em class="replaceable"><code>expr1</code></em>,<em class="replaceable"><code>expr2</code></em>)</code>
</p><p>
Returns <code class="literal">NULL</code> if
<code class="literal"><em class="replaceable"><code>expr1</code></em> =
<em class="replaceable"><code>expr2</code></em></code> is true, otherwise
returns <em class="replaceable"><code>expr1</code></em>. This is the same as
<code class="literal">CASE WHEN <em class="replaceable"><code>expr1</code></em> =
<em class="replaceable"><code>expr2</code></em> THEN NULL ELSE
<em class="replaceable"><code>expr1</code></em> END</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT NULLIF(1,1);</code></strong>
-> NULL
mysql> <strong class="userinput"><code>SELECT NULLIF(1,2);</code></strong>
-> 1
</pre><p>
Note that MySQL evaluates <em class="replaceable"><code>expr1</code></em>
twice if the arguments are not equal.
</p></li></ul></div></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="string-functions"></a>String Functions</h2></div></div></div><div class="toc"><dl><dt><span class="section"><a href="mysqlqb_functions.html#string-comparison-functions">String Comparison Functions</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#regexp">Regular Expressions</a></span></dt></dl></div><a id="id2819240" class="indexterm"></a><a id="id2819247" class="indexterm"></a><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><thead><tr><th><span class="bold"><strong>Name</strong></span></th><th><span class="bold"><strong>Description</strong></span></th></tr></thead><tbody><tr><td><a href="mysqlqb_functions.html#function_ascii"><code class="literal">ASCII()</code></a></td><td>Return numeric value of left-most character</td></tr><tr><td><a href="mysqlqb_functions.html#function_bin"><code class="literal">BIN()</code></a></td><td>Return a string representation of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_bit-length"><code class="literal">BIT_LENGTH()</code></a></td><td>Return length of argument in bits</td></tr><tr><td><a href="mysqlqb_functions.html#function_char-length"><code class="literal">CHAR_LENGTH()</code></a></td><td>Return number of characters in argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_char"><code class="literal">CHAR()</code></a></td><td>Return the character for each integer passed</td></tr><tr><td><a href="mysqlqb_functions.html#function_character-length"><code class="literal">CHARACTER_LENGTH()</code></a></td><td>A synonym for CHAR_LENGTH()</td></tr><tr><td><a href="mysqlqb_functions.html#function_concat-ws"><code class="literal">CONCAT_WS()</code></a></td><td>Return concatenate with separator</td></tr><tr><td><a href="mysqlqb_functions.html#function_concat"><code class="literal">CONCAT()</code></a></td><td>Return concatenated string</td></tr><tr><td><a href="mysqlqb_functions.html#function_conv"><code class="literal">CONV()</code></a></td><td>Convert numbers between different number bases</td></tr><tr><td><a href="mysqlqb_functions.html#function_elt"><code class="literal">ELT()</code></a></td><td>Return string at index number</td></tr><tr><td><a href="mysqlqb_functions.html#operator_equal-to"><code class="literal"><=></code></a></td><td>NULL-safe equal to operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_equal"><code class="literal">=</code></a></td><td>Equal operator</td></tr><tr><td><a href="mysqlqb_functions.html#function_export-set"><code class="literal">EXPORT_SET()</code></a></td><td>Return a string such that for every bit set in the value bits, you get an on string and for every unset bit, you get an off string</td></tr><tr><td><a href="mysqlqb_functions.html#function_field"><code class="literal">FIELD()</code></a></td><td>Return the index (position) of the first argument in the subsequent arguments</td></tr><tr><td><a href="mysqlqb_functions.html#function_find-in-set"><code class="literal">FIND_IN_SET()</code></a></td><td>Return the number of times the first argument is found in the second arguments</td></tr><tr><td><a href="mysqlqb_functions.html#operator_greater-than-or-equal"><code class="literal">>=</code></a></td><td>Greater than or equal operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_greater-than"><code class="literal">></code></a></td><td>Greater than operator</td></tr><tr><td><a href="mysqlqb_functions.html#function_hex"><code class="literal">HEX()</code></a></td><td>Return a string representation of a hex value</td></tr><tr><td><a href="mysqlqb_functions.html#function_insert"><code class="literal">INSERT()</code></a></td><td>Insert a substring at the specified position up to the specified number of characters</td></tr><tr><td><a href="mysqlqb_functions.html#function_instr"><code class="literal">INSTR()</code></a></td><td>Return the index of the first occurrence of substring</td></tr><tr><td><a href="mysqlqb_functions.html#function_is-null"><code class="literal">IS NULL</code></a></td><td>NULL value test</td></tr><tr><td><a href="mysqlqb_functions.html#operator_is"><code class="literal">IS</code></a></td><td>Test a value against a boolean</td></tr><tr><td><a href="mysqlqb_functions.html#function_lcase"><code class="literal">LCASE()</code></a></td><td>Synonym for LOWER() </td></tr><tr><td><a href="mysqlqb_functions.html#function_left"><code class="literal">LEFT()</code></a></td><td>Return the leftmost number of characters as specified</td></tr><tr><td><a href="mysqlqb_functions.html#function_length"><code class="literal">LENGTH()</code></a></td><td>Return the length of a string in bytes</td></tr><tr><td><a href="mysqlqb_functions.html#operator_less-than-or-equal"><code class="literal"><=</code></a></td><td>Less than or equal operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_less-than"><code class="literal"><</code></a></td><td>Less than operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_like"><code class="literal">LIKE</code></a></td><td>Simple pattern matching</td></tr><tr><td><a href="mysqlqb_functions.html#function_load-file"><code class="literal">LOAD_FILE()</code></a></td><td>Load the named file</td></tr><tr><td><a href="mysqlqb_functions.html#function_locate"><code class="literal">LOCATE()</code></a></td><td>Return the position of the first occurrence of substring</td></tr><tr><td><a href="mysqlqb_functions.html#function_lower"><code class="literal">LOWER()</code></a></td><td>Return the argument in lowercase </td></tr><tr><td><a href="mysqlqb_functions.html#function_lpad"><code class="literal">LPAD()</code></a></td><td>Return the string argument, left-padded with the specified string</td></tr><tr><td><a href="mysqlqb_functions.html#function_ltrim"><code class="literal">LTRIM()</code></a></td><td>Remove leading spaces</td></tr><tr><td><a href="mysqlqb_functions.html#function_make-set"><code class="literal">MAKE_SET()</code></a></td><td>Return a set of comma-separated strings that have the corresponding bit in bits set</td></tr><tr><td><a href="mysqlqb_functions.html#function_mid"><code class="literal">MID()</code></a></td><td>Return a substring starting from the specified position</td></tr><tr><td><a href="mysqlqb_functions.html#operator_not-equal"><code class="literal">!=</code>, <code class="literal"><></code></a></td><td>Not equal operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_not-like"><code class="literal">NOT LIKE</code></a></td><td>Negation of simple pattern matching</td></tr><tr><td><a href="mysqlqb_functions.html#operator_not-regexp"><code class="literal">NOT RGEXP</code></a></td><td>Negation of REGEXP</td></tr><tr><td><a href="mysqlqb_functions.html#function_oct"><code class="literal">OCT()</code></a></td><td>Return a string representation of the octal argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_octet-length"><code class="literal">OCTET_LENGTH()</code></a></td><td>A synonym for LENGTH()</td></tr><tr><td><a href="mysqlqb_functions.html#function_ord"><code class="literal">ORD()</code></a></td><td>If the leftmost character of the argument is a multi-byte character, returns the code for that character</td></tr><tr><td><a href="mysqlqb_functions.html#function_position"><code class="literal">POSITION()</code></a></td><td>A synonym for LOCATE()</td></tr><tr><td><a href="mysqlqb_functions.html#function_quote"><code class="literal">QUOTE()</code></a></td><td>Escape the argument for use in an SQL statement</td></tr><tr><td><a href="mysqlqb_functions.html#operator_regexp"><code class="literal">REGEXP</code></a></td><td>Pattern matching using regular expressions</td></tr><tr><td><a href="mysqlqb_functions.html#function_repeat"><code class="literal">REPEAT()</code></a></td><td>Repeat a string the specified number of times</td></tr><tr><td><a href="mysqlqb_functions.html#function_replace"><code class="literal">REPLACE()</code></a></td><td>Replace occurrences of a specified string</td></tr><tr><td><a href="mysqlqb_functions.html#function_reverse"><code class="literal">REVERSE()</code></a></td><td>Reverse the characters in a string</td></tr><tr><td><a href="mysqlqb_functions.html#function_right"><code class="literal">RIGHT()</code></a></td><td>Return the specified rightmost number of characters</td></tr><tr><td><a href="mysqlqb_functions.html#operator_regexp"><code class="literal">RLIKE</code></a></td><td>Synonym for REGEXP</td></tr><tr><td><a href="mysqlqb_functions.html#function_rpad"><code class="literal">RPAD()</code></a></td><td>Append string the specified number of times</td></tr><tr><td><a href="mysqlqb_functions.html#function_rtrim"><code class="literal">RTRIM()</code></a></td><td>Remove trailing spaces</td></tr><tr><td><a href="mysqlqb_functions.html#function_soundex"><code class="literal">SOUNDEX()</code></a></td><td>Return a soundex string</td></tr><tr><td><a href="mysqlqb_functions.html#operator_sounds-like"><code class="literal">SOUNDS LIKE</code></a></td><td>Compare sounds</td></tr><tr><td><a href="mysqlqb_functions.html#function_space"><code class="literal">SPACE()</code></a></td><td>Return a string of the specified number of spaces</td></tr><tr><td><a href="mysqlqb_functions.html#function_strcmp"><code class="literal">STRCMP()</code></a></td><td>Compare two strings</td></tr><tr><td><a href="mysqlqb_functions.html#function_substring-index"><code class="literal">SUBSTRING_INDEX()</code></a></td><td>Return a substring from a string before the specified number of occurrences of the delimiter</td></tr><tr><td><a href="mysqlqb_functions.html#function_substring"><code class="literal">SUBSTRING()</code></a></td><td>Return the substring as specified</td></tr><tr><td><a href="mysqlqb_functions.html#function_trim"><code class="literal">TRIM()</code></a></td><td>Return </td></tr><tr><td><a href="mysqlqb_functions.html#function_ucase"><code class="literal">UCASE()</code></a></td><td>Synonym for UPPER()</td></tr><tr><td><a href="mysqlqb_functions.html#function_unhex"><code class="literal">UNHEX()</code></a></td><td>Convert each pair of hexadecimal digits to a character</td></tr><tr><td><a href="mysqlqb_functions.html#function_upper"><code class="literal">UPPER()</code></a></td><td>Convert to uppercase</td></tr></tbody></table></div><p>
String-valued functions return <code class="literal">NULL</code> if the
length of the result would be greater than the value of the
<code class="literal">max_allowed_packet</code> system variable. See
<a href="http://dev.mysql.com/doc/refman/5.0/en/server-parameters.html" target="_top">Tuning Server Parameters</a>.
</p><p>
For functions that operate on string positions, the first position
is numbered 1.
</p><p>
For functions that take length arguments, non-integer arguments
are rounded to the nearest integer.
</p><div class="itemizedlist"><ul type="disc"><li><p><a id="function_ascii"></a>
<a id="id2820027" class="indexterm"></a>
<code class="literal">ASCII(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
Returns the numeric value of the leftmost character of the
string <em class="replaceable"><code>str</code></em>. Returns
<code class="literal">0</code> if <em class="replaceable"><code>str</code></em> is the
empty string. Returns <code class="literal">NULL</code> if
<em class="replaceable"><code>str</code></em> is <code class="literal">NULL</code>.
<code class="literal">ASCII()</code> works for characters with numeric
values from <code class="literal">0</code> to <code class="literal">255</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT ASCII('2');</code></strong>
-> 50
mysql> <strong class="userinput"><code>SELECT ASCII(2);</code></strong>
-> 50
mysql> <strong class="userinput"><code>SELECT ASCII('dx');</code></strong>
-> 100
</pre><p>
See also the <code class="literal">ORD()</code> function.
</p></li><li><p><a id="function_bin"></a>
<a id="id2820141" class="indexterm"></a>
<code class="literal">BIN(<em class="replaceable"><code>N</code></em>)</code>
</p><p>
Returns a string representation of the binary value of
<em class="replaceable"><code>N</code></em>, where
<em class="replaceable"><code>N</code></em> is a longlong
(<code class="literal">BIGINT</code>) number. This is equivalent to
<code class="literal">CONV(<em class="replaceable"><code>N</code></em>,10,2)</code>.
Returns <code class="literal">NULL</code> if
<em class="replaceable"><code>N</code></em> is <code class="literal">NULL</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT BIN(12);</code></strong>
-> '1100'
</pre></li><li><p><a id="function_bit-length"></a>
<a id="id2820235" class="indexterm"></a>
<code class="literal">BIT_LENGTH(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
Returns the length of the string
<em class="replaceable"><code>str</code></em> in bits.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT BIT_LENGTH('text');</code></strong>
-> 32
</pre></li><li><p><a id="function_char"></a>
<a id="id2820303" class="indexterm"></a>
<code class="literal">CHAR(<em class="replaceable"><code>N</code></em>,... [USING
<em class="replaceable"><code>charset_name</code></em>])</code>
</p><p>
<code class="literal">CHAR()</code> interprets each argument
<em class="replaceable"><code>N</code></em> as an integer and returns a
string consisting of the characters given by the code values
of those integers. <code class="literal">NULL</code> values are skipped.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT CHAR(77,121,83,81,'76');</code></strong>
-> 'MySQL'
mysql> <strong class="userinput"><code>SELECT CHAR(77,77.3,'77.3');</code></strong>
-> 'MMM'
</pre><p>
As of MySQL 5.0.15, <code class="literal">CHAR()</code> arguments larger
than 255 are converted into multiple result bytes. For
example, <code class="literal">CHAR(256)</code> is equivalent to
<code class="literal">CHAR(1,0)</code>, and
<code class="literal">CHAR(256*256)</code> is equivalent to
<code class="literal">CHAR(1,0,0)</code>:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT HEX(CHAR(1,0)), HEX(CHAR(256));</code></strong>
+----------------+----------------+
| HEX(CHAR(1,0)) | HEX(CHAR(256)) |
+----------------+----------------+
| 0100 | 0100 |
+----------------+----------------+
mysql> <strong class="userinput"><code>SELECT HEX(CHAR(1,0,0)), HEX(CHAR(256*256));</code></strong>
+------------------+--------------------+
| HEX(CHAR(1,0,0)) | HEX(CHAR(256*256)) |
+------------------+--------------------+
| 010000 | 010000 |
+------------------+--------------------+
</pre><p>
By default, <code class="literal">CHAR()</code> returns a binary string.
To produce a string in a given character set, use the optional
<code class="literal">USING</code> clause:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT CHARSET(CHAR(0x65)), CHARSET(CHAR(0x65 USING utf8));</code></strong>
+---------------------+--------------------------------+
| CHARSET(CHAR(0x65)) | CHARSET(CHAR(0x65 USING utf8)) |
+---------------------+--------------------------------+
| binary | utf8 |
+---------------------+--------------------------------+
</pre><p>
If <code class="literal">USING</code> is given and the result string is
illegal for the given character set, a warning is issued.
Also, if strict SQL mode is enabled, the result from
<code class="literal">CHAR()</code> becomes <code class="literal">NULL</code>.
</p><p>
Before MySQL 5.0.15, <code class="literal">CHAR()</code> returns a
string in the connection character set and the
<code class="literal">USING</code> clause is unavailable. In addition,
each argument is interpreted modulo 256, so
<code class="literal">CHAR(256)</code> and
<code class="literal">CHAR(256*256)</code> both are equivalent to
<code class="literal">CHAR(0)</code>.
</p></li><li><p><a id="function_char-length"></a>
<a id="id2820515" class="indexterm"></a>
<code class="literal">CHAR_LENGTH(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
Returns the length of the string
<em class="replaceable"><code>str</code></em>, measured in characters. A
multi-byte character counts as a single character. This means
that for a string containing five two-byte characters,
<code class="literal">LENGTH()</code> returns <code class="literal">10</code>,
whereas <code class="literal">CHAR_LENGTH()</code> returns
<code class="literal">5</code>.
</p></li><li><p><a id="function_character-length"></a>
<a id="id2820588" class="indexterm"></a>
<code class="literal">CHARACTER_LENGTH(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
<code class="literal">CHARACTER_LENGTH()</code> is a synonym for
<code class="literal">CHAR_LENGTH()</code>.
</p></li><li><p><a id="function_concat"></a>
<a id="id2820647" class="indexterm"></a>
<code class="literal">CONCAT(<em class="replaceable"><code>str1</code></em>,<em class="replaceable"><code>str2</code></em>,...)</code>
</p><p>
Returns the string that results from concatenating the
arguments. May have one or more arguments. If all arguments
are non-binary strings, the result is a non-binary string. If
the arguments include any binary strings, the result is a
binary string. A numeric argument is converted to its
equivalent binary string form; if you want to avoid that, you
can use an explicit type cast, as in this example:
</p><pre class="programlisting">SELECT CONCAT(CAST(<em class="replaceable"><code>int_col</code></em> AS CHAR), <em class="replaceable"><code>char_col</code></em>);
</pre><p>
<code class="literal">CONCAT()</code> returns <code class="literal">NULL</code> if
any argument is <code class="literal">NULL</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT CONCAT('My', 'S', 'QL');</code></strong>
-> 'MySQL'
mysql> <strong class="userinput"><code>SELECT CONCAT('My', NULL, 'QL');</code></strong>
-> NULL
mysql> <strong class="userinput"><code>SELECT CONCAT(14.3);</code></strong>
-> '14.3'
</pre></li><li><p><a id="function_concat-ws"></a>
<a id="id2820757" class="indexterm"></a>
<code class="literal">CONCAT_WS(<em class="replaceable"><code>separator</code></em>,<em class="replaceable"><code>str1</code></em>,<em class="replaceable"><code>str2</code></em>,...)</code>
</p><p>
<code class="literal">CONCAT_WS()</code> stands for Concatenate With
Separator and is a special form of
<code class="literal">CONCAT()</code>. The first argument is the
separator for the rest of the arguments. The separator is
added between the strings to be concatenated. The separator
can be a string, as can the rest of the arguments. If the
separator is <code class="literal">NULL</code>, the result is
<code class="literal">NULL</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT CONCAT_WS(',','First name','Second name','Last Name');</code></strong>
-> 'First name,Second name,Last Name'
mysql> <strong class="userinput"><code>SELECT CONCAT_WS(',','First name',NULL,'Last Name');</code></strong>
-> 'First name,Last Name'
</pre><p>
<code class="literal">CONCAT_WS()</code> does not skip empty strings.
However, it does skip any <code class="literal">NULL</code> values after
the separator argument.
</p></li><li><p><a id="function_conv"></a>
<a id="id2820869" class="indexterm"></a>
<code class="literal">CONV(<em class="replaceable"><code>N</code></em>,<em class="replaceable"><code>from_base</code></em>,<em class="replaceable"><code>to_base</code></em>)</code>
</p><p>
Converts numbers between different number bases. Returns a
string representation of the number
<em class="replaceable"><code>N</code></em>, converted from base
<em class="replaceable"><code>from_base</code></em> to base
<em class="replaceable"><code>to_base</code></em>. Returns
<code class="literal">NULL</code> if any argument is
<code class="literal">NULL</code>. The argument
<em class="replaceable"><code>N</code></em> is interpreted as an integer, but
may be specified as an integer or a string. The minimum base
is <code class="literal">2</code> and the maximum base is
<code class="literal">36</code>. If <em class="replaceable"><code>to_base</code></em>
is a negative number, <em class="replaceable"><code>N</code></em> is regarded
as a signed number. Otherwise, <em class="replaceable"><code>N</code></em> is
treated as unsigned. <code class="literal">CONV()</code> works with
64-bit precision.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT CONV('a',16,2);</code></strong>
-> '1010'
mysql> <strong class="userinput"><code>SELECT CONV('6E',18,8);</code></strong>
-> '172'
mysql> <strong class="userinput"><code>SELECT CONV(-17,10,-18);</code></strong>
-> '-H'
mysql> <strong class="userinput"><code>SELECT CONV(10+'10'+'10'+0xa,10,10);</code></strong>
-> '40'
</pre></li><li><p><a id="function_elt"></a>
<a id="id2821005" class="indexterm"></a>
<code class="literal">ELT(<em class="replaceable"><code>N</code></em>,<em class="replaceable"><code>str1</code></em>,<em class="replaceable"><code>str2</code></em>,<em class="replaceable"><code>str3</code></em>,...)</code>
</p><p>
Returns <em class="replaceable"><code>str1</code></em> if
<em class="replaceable"><code>N</code></em> = <code class="literal">1</code>,
<em class="replaceable"><code>str2</code></em> if
<em class="replaceable"><code>N</code></em> = <code class="literal">2</code>, and so
on. Returns <code class="literal">NULL</code> if
<em class="replaceable"><code>N</code></em> is less than <code class="literal">1</code>
or greater than the number of arguments.
<code class="literal">ELT()</code> is the complement of
<code class="literal">FIELD()</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT ELT(1, 'ej', 'Heja', 'hej', 'foo');</code></strong>
-> 'ej'
mysql> <strong class="userinput"><code>SELECT ELT(4, 'ej', 'Heja', 'hej', 'foo');</code></strong>
-> 'foo'
</pre></li><li><p><a id="function_export-set"></a>
<a id="id2821126" class="indexterm"></a>
<code class="literal">EXPORT_SET(<em class="replaceable"><code>bits</code></em>,<em class="replaceable"><code>on</code></em>,<em class="replaceable"><code>off</code></em>[,<em class="replaceable"><code>separator</code></em>[,<em class="replaceable"><code>number_of_bits</code></em>]])</code>
</p><p>
Returns a string such that for every bit set in the value
<em class="replaceable"><code>bits</code></em>, you get an
<em class="replaceable"><code>on</code></em> string and for every bit not set
in the value, you get an <em class="replaceable"><code>off</code></em>
string. Bits in <em class="replaceable"><code>bits</code></em> are examined
from right to left (from low-order to high-order bits).
Strings are added to the result from left to right, separated
by the <em class="replaceable"><code>separator</code></em> string (the
default being the comma character
‘<code class="literal">,</code>’). The number of bits
examined is given by <em class="replaceable"><code>number_of_bits</code></em>
(defaults to 64).
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT EXPORT_SET(5,'Y','N',',',4);</code></strong>
-> 'Y,N,Y,N'
mysql> <strong class="userinput"><code>SELECT EXPORT_SET(6,'1','0',',',10);</code></strong>
-> '0,1,1,0,0,0,0,0,0,0'
</pre></li><li><p><a id="function_field"></a>
<a id="id2821244" class="indexterm"></a>
<code class="literal">FIELD(<em class="replaceable"><code>str</code></em>,<em class="replaceable"><code>str1</code></em>,<em class="replaceable"><code>str2</code></em>,<em class="replaceable"><code>str3</code></em>,...)</code>
</p><p>
Returns the index (position) of <em class="replaceable"><code>str</code></em>
in the <em class="replaceable"><code>str1</code></em>,
<em class="replaceable"><code>str2</code></em>,
<em class="replaceable"><code>str3</code></em>, <code class="literal">...</code> list.
Returns <code class="literal">0</code> if <em class="replaceable"><code>str</code></em>
is not found.
</p><p>
If all arguments to <code class="literal">FIELD()</code> are strings,
all arguments are compared as strings. If all arguments are
numbers, they are compared as numbers. Otherwise, the
arguments are compared as double.
</p><p>
If <em class="replaceable"><code>str</code></em> is <code class="literal">NULL</code>,
the return value is <code class="literal">0</code> because
<code class="literal">NULL</code> fails equality comparison with any
value. <code class="literal">FIELD()</code> is the complement of
<code class="literal">ELT()</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT FIELD('ej', 'Hej', 'ej', 'Heja', 'hej', 'foo');</code></strong>
-> 2
mysql> <strong class="userinput"><code>SELECT FIELD('fo', 'Hej', 'ej', 'Heja', 'hej', 'foo');</code></strong>
-> 0
</pre></li><li><p><a id="function_find-in-set"></a>
<a id="id2821388" class="indexterm"></a>
<code class="literal">FIND_IN_SET(<em class="replaceable"><code>str</code></em>,<em class="replaceable"><code>strlist</code></em>)</code>
</p><p>
Returns a value in the range of 1 to
<em class="replaceable"><code>N</code></em> if the string
<em class="replaceable"><code>str</code></em> is in the string list
<em class="replaceable"><code>strlist</code></em> consisting of
<em class="replaceable"><code>N</code></em> substrings. A string list is a
string composed of substrings separated by
‘<code class="literal">,</code>’ characters. If the first
argument is a constant string and the second is a column of
type <code class="literal">SET</code>, the
<code class="literal">FIND_IN_SET()</code> function is optimized to use
bit arithmetic. Returns <code class="literal">0</code> if
<em class="replaceable"><code>str</code></em> is not in
<em class="replaceable"><code>strlist</code></em> or if
<em class="replaceable"><code>strlist</code></em> is the empty string.
Returns <code class="literal">NULL</code> if either argument is
<code class="literal">NULL</code>. This function does not work properly
if the first argument contains a comma
(‘<code class="literal">,</code>’) character.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT FIND_IN_SET('b','a,b,c,d');</code></strong>
-> 2
</pre></li><li><p><a id="function_format"></a>
<a id="id2821518" class="indexterm"></a>
<code class="literal">FORMAT(<em class="replaceable"><code>X</code></em>,<em class="replaceable"><code>D</code></em>)</code>
</p><p>
Formats the number <em class="replaceable"><code>X</code></em> to a format
like <code class="literal">'#,###,###.##'</code>, rounded to
<em class="replaceable"><code>D</code></em> decimal places, and returns the
result as a string. If <em class="replaceable"><code>D</code></em> is
<code class="literal">0</code>, the result has no decimal point or
fractional part.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT FORMAT(12332.123456, 4);</code></strong>
-> '12,332.1235'
mysql> <strong class="userinput"><code>SELECT FORMAT(12332.1,4);</code></strong>
-> '12,332.1000'
mysql> <strong class="userinput"><code>SELECT FORMAT(12332.2,0);</code></strong>
-> '12,332'
</pre></li><li><p><a id="function_hex"></a>
<a id="id2821615" class="indexterm"></a>
<code class="literal">HEX(<em class="replaceable"><code>N_or_S</code></em>)</code>
</p><p>
If <em class="replaceable"><code>N_or_S</code></em> is a number, returns a
string representation of the hexadecimal value of
<em class="replaceable"><code>N</code></em>, where
<em class="replaceable"><code>N</code></em> is a longlong
(<code class="literal">BIGINT</code>) number. This is equivalent to
<code class="literal">CONV(<em class="replaceable"><code>N</code></em>,10,16)</code>.
</p><p>
If <em class="replaceable"><code>N_or_S</code></em> is a string, returns a
hexadecimal string representation of
<em class="replaceable"><code>N_or_S</code></em> where each character in
<em class="replaceable"><code>N_or_S</code></em> is converted to two
hexadecimal digits.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT HEX(255);</code></strong>
-> 'FF'
mysql> <strong class="userinput"><code>SELECT 0x616263;</code></strong>
-> 'abc'
mysql> <strong class="userinput"><code>SELECT HEX('abc');</code></strong>
-> 616263
</pre></li><li><p><a id="function_insert"></a>
<a id="id2821735" class="indexterm"></a>
<code class="literal">INSERT(<em class="replaceable"><code>str</code></em>,<em class="replaceable"><code>pos</code></em>,<em class="replaceable"><code>len</code></em>,<em class="replaceable"><code>newstr</code></em>)</code>
</p><p>
Returns the string <em class="replaceable"><code>str</code></em>, with the
substring beginning at position <em class="replaceable"><code>pos</code></em>
and <em class="replaceable"><code>len</code></em> characters long replaced by
the string <em class="replaceable"><code>newstr</code></em>. Returns the
original string if <em class="replaceable"><code>pos</code></em> is not
within the length of the string. Replaces the rest of the
string from position <em class="replaceable"><code>pos</code></em> is
<em class="replaceable"><code>len</code></em> is not within the length of the
rest of the string. Returns <code class="literal">NULL</code> if any
argument is <code class="literal">NULL</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT INSERT('Quadratic', 3, 4, 'What');</code></strong>
-> 'QuWhattic'
mysql> <strong class="userinput"><code>SELECT INSERT('Quadratic', -1, 4, 'What');</code></strong>
-> 'Quadratic'
mysql> <strong class="userinput"><code>SELECT INSERT('Quadratic', 3, 100, 'What');</code></strong>
-> 'QuWhat'
</pre><p>
This function is multi-byte safe.
</p></li><li><p><a id="function_instr"></a>
<a id="id2821861" class="indexterm"></a>
<code class="literal">INSTR(<em class="replaceable"><code>str</code></em>,<em class="replaceable"><code>substr</code></em>)</code>
</p><p>
Returns the position of the first occurrence of substring
<em class="replaceable"><code>substr</code></em> in string
<em class="replaceable"><code>str</code></em>. This is the same as the
two-argument form of <code class="literal">LOCATE()</code>, except that
the order of the arguments is reversed.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT INSTR('foobarbar', 'bar');</code></strong>
-> 4
mysql> <strong class="userinput"><code>SELECT INSTR('xbar', 'foobar');</code></strong>
-> 0
</pre><p>
This function is multi-byte safe, and is case sensitive only
if at least one argument is a binary string.
</p></li><li><p><a id="function_lcase"></a>
<a id="id2821952" class="indexterm"></a>
<code class="literal">LCASE(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
<code class="literal">LCASE()</code> is a synonym for
<code class="literal">LOWER()</code>.
</p></li><li><p><a id="function_left"></a>
<a id="id2822010" class="indexterm"></a>
<code class="literal">LEFT(<em class="replaceable"><code>str</code></em>,<em class="replaceable"><code>len</code></em>)</code>
</p><p>
Returns the leftmost <em class="replaceable"><code>len</code></em> characters
from the string <em class="replaceable"><code>str</code></em>, or
<code class="literal">NULL</code> if any argument is
<code class="literal">NULL</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT LEFT('foobarbar', 5);</code></strong>
-> 'fooba'
</pre></li><li><p><a id="function_length"></a>
<a id="id2822093" class="indexterm"></a>
<code class="literal">LENGTH(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
Returns the length of the string
<em class="replaceable"><code>str</code></em>, measured in bytes. A
multi-byte character counts as multiple bytes. This means that
for a string containing five two-byte characters,
<code class="literal">LENGTH()</code> returns <code class="literal">10</code>,
whereas <code class="literal">CHAR_LENGTH()</code> returns
<code class="literal">5</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT LENGTH('text');</code></strong>
-> 4
</pre></li><li><p><a id="function_load-file"></a>
<a id="id2822178" class="indexterm"></a>
<a id="id2822184" class="indexterm"></a>
<code class="literal">LOAD_FILE(<em class="replaceable"><code>file_name</code></em>)</code>
</p><p>
Reads the file and returns the file contents as a string. To
use this function, the file must be located on the server
host, you must specify the full pathname to the file, and you
must have the <code class="literal">FILE</code> privilege. The file must
be readable by all and its size less than
<code class="literal">max_allowed_packet</code> bytes.
</p><p>
If the file does not exist or cannot be read because one of
the preceding conditions is not satisfied, the function
returns <code class="literal">NULL</code>.
</p><p>
As of MySQL 5.0.19, the
<code class="literal">character_set_filesystem</code> system variable
controls interpretation of filenames that are given as literal
strings.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>UPDATE t</code></strong>
<strong class="userinput"><code> SET blob_col=LOAD_FILE('/tmp/picture')</code></strong>
<strong class="userinput"><code> WHERE id=1;</code></strong>
</pre></li><li><p><a id="function_locate"></a>
<a id="id2822287" class="indexterm"></a>
<code class="literal">LOCATE(<em class="replaceable"><code>substr</code></em>,<em class="replaceable"><code>str</code></em>)</code>,
<code class="literal">LOCATE(<em class="replaceable"><code>substr</code></em>,<em class="replaceable"><code>str</code></em>,<em class="replaceable"><code>pos</code></em>)</code>
</p><p>
The first syntax returns the position of the first occurrence
of substring <em class="replaceable"><code>substr</code></em> in string
<em class="replaceable"><code>str</code></em>. The second syntax returns the
position of the first occurrence of substring
<em class="replaceable"><code>substr</code></em> in string
<em class="replaceable"><code>str</code></em>, starting at position
<em class="replaceable"><code>pos</code></em>. Returns <code class="literal">0</code>
if <em class="replaceable"><code>substr</code></em> is not in
<em class="replaceable"><code>str</code></em>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT LOCATE('bar', 'foobarbar');</code></strong>
-> 4
mysql> <strong class="userinput"><code>SELECT LOCATE('xbar', 'foobar');</code></strong>
-> 0
mysql> <strong class="userinput"><code>SELECT LOCATE('bar', 'foobarbar', 5);</code></strong>
-> 7
</pre><p>
This function is multi-byte safe, and is case-sensitive only
if at least one argument is a binary string.
</p></li><li><p><a id="function_lower"></a>
<a id="id2822415" class="indexterm"></a>
<code class="literal">LOWER(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
Returns the string <em class="replaceable"><code>str</code></em> with all
characters changed to lowercase according to the current
character set mapping. The default is
<code class="literal">latin1</code> (cp1252 West European).
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT LOWER('QUADRATICALLY');</code></strong>
-> 'quadratically'
</pre><p>
This function is multi-byte safe.
</p></li><li><p><a id="function_lpad"></a>
<a id="id2822493" class="indexterm"></a>
<code class="literal">LPAD(<em class="replaceable"><code>str</code></em>,<em class="replaceable"><code>len</code></em>,<em class="replaceable"><code>padstr</code></em>)</code>
</p><p>
Returns the string <em class="replaceable"><code>str</code></em>, left-padded
with the string <em class="replaceable"><code>padstr</code></em> to a length
of <em class="replaceable"><code>len</code></em> characters. If
<em class="replaceable"><code>str</code></em> is longer than
<em class="replaceable"><code>len</code></em>, the return value is shortened
to <em class="replaceable"><code>len</code></em> characters.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT LPAD('hi',4,'??');</code></strong>
-> '??hi'
mysql> <strong class="userinput"><code>SELECT LPAD('hi',1,'??');</code></strong>
-> 'h'
</pre></li><li><p><a id="function_ltrim"></a>
<a id="id2822592" class="indexterm"></a>
<code class="literal">LTRIM(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
Returns the string <em class="replaceable"><code>str</code></em> with leading
space characters removed.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT LTRIM(' barbar');</code></strong>
-> 'barbar'
</pre><p>
This function is multi-byte safe.
</p></li><li><p><a id="function_make-set"></a>
<a id="id2822664" class="indexterm"></a>
<code class="literal">MAKE_SET(<em class="replaceable"><code>bits</code></em>,<em class="replaceable"><code>str1</code></em>,<em class="replaceable"><code>str2</code></em>,...)</code>
</p><p>
Returns a set value (a string containing substrings separated
by ‘<code class="literal">,</code>’ characters) consisting
of the strings that have the corresponding bit in
<em class="replaceable"><code>bits</code></em> set.
<em class="replaceable"><code>str1</code></em> corresponds to bit 0,
<em class="replaceable"><code>str2</code></em> to bit 1, and so on.
<code class="literal">NULL</code> values in
<em class="replaceable"><code>str1</code></em>,
<em class="replaceable"><code>str2</code></em>, <code class="literal">...</code> are
not appended to the result.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT MAKE_SET(1,'a','b','c');</code></strong>
-> 'a'
mysql> <strong class="userinput"><code>SELECT MAKE_SET(1 | 4,'hello','nice','world');</code></strong>
-> 'hello,world'
mysql> <strong class="userinput"><code>SELECT MAKE_SET(1 | 4,'hello','nice',NULL,'world');</code></strong>
-> 'hello'
mysql> <strong class="userinput"><code>SELECT MAKE_SET(0,'a','b','c');</code></strong>
-> ''
</pre></li><li><p><a id="function_mid"></a>
<a id="id2822785" class="indexterm"></a>
<code class="literal">MID(<em class="replaceable"><code>str</code></em>,<em class="replaceable"><code>pos</code></em>,<em class="replaceable"><code>len</code></em>)</code>
</p><p>
<code class="literal">MID(<em class="replaceable"><code>str</code></em>,<em class="replaceable"><code>pos</code></em>,<em class="replaceable"><code>len</code></em>)</code>
is a synonym for
<code class="literal">SUBSTRING(<em class="replaceable"><code>str</code></em>,<em class="replaceable"><code>pos</code></em>,<em class="replaceable"><code>len</code></em>)</code>.
</p></li><li><p><a id="function_oct"></a>
<a id="id2822869" class="indexterm"></a>
<code class="literal">OCT(<em class="replaceable"><code>N</code></em>)</code>
</p><p>
Returns a string representation of the octal value of
<em class="replaceable"><code>N</code></em>, where
<em class="replaceable"><code>N</code></em> is a longlong
(<code class="literal">BIGINT</code>) number. This is equivalent to
<code class="literal">CONV(<em class="replaceable"><code>N</code></em>,10,8)</code>.
Returns <code class="literal">NULL</code> if
<em class="replaceable"><code>N</code></em> is <code class="literal">NULL</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT OCT(12);</code></strong>
-> '14'
</pre></li><li><p><a id="function_octet-length"></a>
<a id="id2822964" class="indexterm"></a>
<code class="literal">OCTET_LENGTH(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
<code class="literal">OCTET_LENGTH()</code> is a synonym for
<code class="literal">LENGTH()</code>.
</p></li><li><p><a id="function_ord"></a>
<a id="id2823022" class="indexterm"></a>
<code class="literal">ORD(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
If the leftmost character of the string
<em class="replaceable"><code>str</code></em> is a multi-byte character,
returns the code for that character, calculated from the
numeric values of its constituent bytes using this formula:
</p><pre class="programlisting"> (1st byte code)
+ (2nd byte code × 256)
+ (3rd byte code × 256<sup>2</sup>) ...
</pre><p>
If the leftmost character is not a multi-byte character,
<code class="literal">ORD()</code> returns the same value as the
<code class="literal">ASCII()</code> function.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT ORD('2');</code></strong>
-> 50
</pre></li><li><p><a id="function_position"></a>
<a id="id2823114" class="indexterm"></a>
<code class="literal">POSITION(<em class="replaceable"><code>substr</code></em> IN
<em class="replaceable"><code>str</code></em>)</code>
</p><p>
<code class="literal">POSITION(<em class="replaceable"><code>substr</code></em> IN
<em class="replaceable"><code>str</code></em>)</code> is a synonym for
<code class="literal">LOCATE(<em class="replaceable"><code>substr</code></em>,<em class="replaceable"><code>str</code></em>)</code>.
</p></li><li><p><a id="function_quote"></a>
<a id="id2823190" class="indexterm"></a>
<code class="literal">QUOTE(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
Quotes a string to produce a result that can be used as a
properly escaped data value in an SQL statement. The string is
returned enclosed by single quotes and with each instance of
single quote (‘<code class="literal">'</code>’), backslash
(‘<code class="literal">\</code>’), ASCII
<code class="literal">NUL</code>, and Control-Z preceded by a backslash.
If the argument is <code class="literal">NULL</code>, the return value
is the word “<span class="quote">NULL</span>” without enclosing single
quotes.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT QUOTE('Don\'t!');</code></strong>
-> 'Don\'t!'
mysql> <strong class="userinput"><code>SELECT QUOTE(NULL);</code></strong>
-> NULL
</pre></li><li><p><a id="function_repeat"></a>
<a id="id2823287" class="indexterm"></a>
<code class="literal">REPEAT(<em class="replaceable"><code>str</code></em>,<em class="replaceable"><code>count</code></em>)</code>
</p><p>
Returns a string consisting of the string
<em class="replaceable"><code>str</code></em> repeated
<em class="replaceable"><code>count</code></em> times. If
<em class="replaceable"><code>count</code></em> is less than 1, returns an
empty string. Returns <code class="literal">NULL</code> if
<em class="replaceable"><code>str</code></em> or
<em class="replaceable"><code>count</code></em> are <code class="literal">NULL</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT REPEAT('MySQL', 3);</code></strong>
-> 'MySQLMySQLMySQL'
</pre></li><li><p><a id="function_replace"></a>
<a id="id2823383" class="indexterm"></a>
<code class="literal">REPLACE(<em class="replaceable"><code>str</code></em>,<em class="replaceable"><code>from_str</code></em>,<em class="replaceable"><code>to_str</code></em>)</code>
</p><p>
Returns the string <em class="replaceable"><code>str</code></em> with all
occurrences of the string <em class="replaceable"><code>from_str</code></em>
replaced by the string <em class="replaceable"><code>to_str</code></em>.
<code class="literal">REPLACE()</code> performs a case-sensitive match
when searching for <em class="replaceable"><code>from_str</code></em>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT REPLACE('www.mysql.com', 'w', 'Ww');</code></strong>
-> 'WwWwWw.mysql.com'
</pre><p>
This function is multi-byte safe.
</p></li><li><p><a id="function_reverse"></a>
<a id="id2823480" class="indexterm"></a>
<code class="literal">REVERSE(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
Returns the string <em class="replaceable"><code>str</code></em> with the
order of the characters reversed.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT REVERSE('abc');</code></strong>
-> 'cba'
</pre><p>
This function is multi-byte safe.
</p></li><li><p><a id="function_right"></a>
<a id="id2823552" class="indexterm"></a>
<code class="literal">RIGHT(<em class="replaceable"><code>str</code></em>,<em class="replaceable"><code>len</code></em>)</code>
</p><p>
Returns the rightmost <em class="replaceable"><code>len</code></em>
characters from the string <em class="replaceable"><code>str</code></em>, or
<code class="literal">NULL</code> if any argument is
<code class="literal">NULL</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT RIGHT('foobarbar', 4);</code></strong>
-> 'rbar'
</pre><p>
This function is multi-byte safe.
</p></li><li><p><a id="function_rpad"></a>
<a id="id2823638" class="indexterm"></a>
<code class="literal">RPAD(<em class="replaceable"><code>str</code></em>,<em class="replaceable"><code>len</code></em>,<em class="replaceable"><code>padstr</code></em>)</code>
</p><p>
Returns the string <em class="replaceable"><code>str</code></em>,
right-padded with the string <em class="replaceable"><code>padstr</code></em>
to a length of <em class="replaceable"><code>len</code></em> characters. If
<em class="replaceable"><code>str</code></em> is longer than
<em class="replaceable"><code>len</code></em>, the return value is shortened
to <em class="replaceable"><code>len</code></em> characters.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT RPAD('hi',5,'?');</code></strong>
-> 'hi???'
mysql> <strong class="userinput"><code>SELECT RPAD('hi',1,'?');</code></strong>
-> 'h'
</pre><p>
This function is multi-byte safe.
</p></li><li><p><a id="function_rtrim"></a>
<a id="id2823741" class="indexterm"></a>
<code class="literal">RTRIM(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
Returns the string <em class="replaceable"><code>str</code></em> with
trailing space characters removed.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT RTRIM('barbar ');</code></strong>
-> 'barbar'
</pre><p>
This function is multi-byte safe.
</p></li><li><p><a id="function_soundex"></a>
<a id="id2823813" class="indexterm"></a>
<code class="literal">SOUNDEX(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
Returns a soundex string from <em class="replaceable"><code>str</code></em>.
Two strings that sound almost the same should have identical
soundex strings. A standard soundex string is four characters
long, but the <code class="literal">SOUNDEX()</code> function returns an
arbitrarily long string. You can use
<code class="literal">SUBSTRING()</code> on the result to get a standard
soundex string. All non-alphabetic characters in
<em class="replaceable"><code>str</code></em> are ignored. All international
alphabetic characters outside the A-Z range are treated as
vowels.
</p><p>
<span class="bold"><strong>Important</strong></span>: When using
<code class="literal">SOUNDEX()</code>, you should be aware of the
following limitations:
</p><div class="itemizedlist"><ul type="circle"><li><p>
This function, as currently implemented, is intended to
work well with strings that are in the English language
only. Strings in other languages may not produce reliable
results.
</p></li><li><p>
This function is not guaranteed to provide consistent
results with strings that use multi-byte character sets,
including <code class="literal">utf-8</code>.
</p><p>
We hope to remove these limitations in a future release.
See <a href="http://bugs.mysql.com/22638" target="_top">Bug#22638</a> for more information.
</p></li></ul></div><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT SOUNDEX('Hello');</code></strong>
-> 'H400'
mysql> <strong class="userinput"><code>SELECT SOUNDEX('Quadratically');</code></strong>
-> 'Q36324'
</pre><p>
<span class="bold"><strong>Note</strong></span>: This function
implements the original Soundex algorithm, not the more
popular enhanced version (also described by D. Knuth). The
difference is that original version discards vowels first and
duplicates second, whereas the enhanced version discards
duplicates first and vowels second.
</p></li><li><p><a id="operator_sounds-like"></a>
<a id="id2823978" class="indexterm"></a>
<code class="literal"><em class="replaceable"><code>expr1</code></em> SOUNDS LIKE
<em class="replaceable"><code>expr2</code></em></code>
</p><p>
This is the same as
<code class="literal">SOUNDEX(<em class="replaceable"><code>expr1</code></em>) =
SOUNDEX(<em class="replaceable"><code>expr2</code></em>)</code>.
</p></li><li><p><a id="function_space"></a>
<a id="id2824042" class="indexterm"></a>
<code class="literal">SPACE(<em class="replaceable"><code>N</code></em>)</code>
</p><p>
Returns a string consisting of <em class="replaceable"><code>N</code></em>
space characters.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT SPACE(6);</code></strong>
-> ' '
</pre></li><li><p><a id="function_substring"></a>
<a id="id2824110" class="indexterm"></a>
<a id="id2824116" class="indexterm"></a>
<code class="literal">SUBSTRING(<em class="replaceable"><code>str</code></em>,<em class="replaceable"><code>pos</code></em>)</code>,
<code class="literal">SUBSTRING(<em class="replaceable"><code>str</code></em> FROM
<em class="replaceable"><code>pos</code></em>)</code>,
<code class="literal">SUBSTRING(<em class="replaceable"><code>str</code></em>,<em class="replaceable"><code>pos</code></em>,<em class="replaceable"><code>len</code></em>)</code>,
<code class="literal">SUBSTRING(<em class="replaceable"><code>str</code></em> FROM
<em class="replaceable"><code>pos</code></em> FOR
<em class="replaceable"><code>len</code></em>)</code>
</p><p>
The forms without a <em class="replaceable"><code>len</code></em> argument
return a substring from string <em class="replaceable"><code>str</code></em>
starting at position <em class="replaceable"><code>pos</code></em>. The forms
with a <em class="replaceable"><code>len</code></em> argument return a
substring <em class="replaceable"><code>len</code></em> characters long from
string <em class="replaceable"><code>str</code></em>, starting at position
<em class="replaceable"><code>pos</code></em>. The forms that use
<code class="literal">FROM</code> are standard SQL syntax. It is also
possible to use a negative value for
<em class="replaceable"><code>pos</code></em>. In this case, the beginning of
the substring is <em class="replaceable"><code>pos</code></em> characters
from the end of the string, rather than the beginning. A
negative value may be used for <em class="replaceable"><code>pos</code></em>
in any of the forms of this function.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT SUBSTRING('Quadratically',5);</code></strong>
-> 'ratically'
mysql> <strong class="userinput"><code>SELECT SUBSTRING('foobarbar' FROM 4);</code></strong>
-> 'barbar'
mysql> <strong class="userinput"><code>SELECT SUBSTRING('Quadratically',5,6);</code></strong>
-> 'ratica'
mysql> <strong class="userinput"><code>SELECT SUBSTRING('Sakila', -3);</code></strong>
-> 'ila'
mysql> <strong class="userinput"><code>SELECT SUBSTRING('Sakila', -5, 3);</code></strong>
-> 'aki'
mysql> <strong class="userinput"><code>SELECT SUBSTRING('Sakila' FROM -4 FOR 2);</code></strong>
-> 'ki'
</pre><p>
This function is multi-byte safe.
</p><p>
If <em class="replaceable"><code>len</code></em> is less than 1, the result
is the empty string.
</p><p>
<code class="literal">SUBSTR()</code> is a synonym for
<code class="literal">SUBSTRING()</code>.
</p></li><li><p><a id="function_substring-index"></a>
<a id="id2824319" class="indexterm"></a>
<code class="literal">SUBSTRING_INDEX(<em class="replaceable"><code>str</code></em>,<em class="replaceable"><code>delim</code></em>,<em class="replaceable"><code>count</code></em>)</code>
</p><p>
Returns the substring from string
<em class="replaceable"><code>str</code></em> before
<em class="replaceable"><code>count</code></em> occurrences of the delimiter
<em class="replaceable"><code>delim</code></em>. If
<em class="replaceable"><code>count</code></em> is positive, everything to
the left of the final delimiter (counting from the left) is
returned. If <em class="replaceable"><code>count</code></em> is negative,
everything to the right of the final delimiter (counting from
the right) is returned. <code class="literal">SUBSTRING_INDEX()</code>
performs a case-sensitive match when searching for
<em class="replaceable"><code>delim</code></em>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT SUBSTRING_INDEX('www.mysql.com', '.', 2);</code></strong>
-> 'www.mysql'
mysql> <strong class="userinput"><code>SELECT SUBSTRING_INDEX('www.mysql.com', '.', -2);</code></strong>
-> 'mysql.com'
</pre><p>
This function is multi-byte safe.
</p></li><li><p><a id="function_trim"></a>
<a id="id2824439" class="indexterm"></a>
<code class="literal">TRIM([{BOTH | LEADING | TRAILING}
[<em class="replaceable"><code>remstr</code></em>] FROM]
<em class="replaceable"><code>str</code></em>)</code>,
<code class="literal">TRIM([<em class="replaceable"><code>remstr</code></em> FROM]
<em class="replaceable"><code>str</code></em>)</code>
</p><p>
Returns the string <em class="replaceable"><code>str</code></em> with all
<em class="replaceable"><code>remstr</code></em> prefixes or suffixes
removed. If none of the specifiers <code class="literal">BOTH</code>,
<code class="literal">LEADING</code>, or <code class="literal">TRAILING</code> is
given, <code class="literal">BOTH</code> is assumed.
<em class="replaceable"><code>remstr</code></em> is optional and, if not
specified, spaces are removed.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT TRIM(' bar ');</code></strong>
-> 'bar'
mysql> <strong class="userinput"><code>SELECT TRIM(LEADING 'x' FROM 'xxxbarxxx');</code></strong>
-> 'barxxx'
mysql> <strong class="userinput"><code>SELECT TRIM(BOTH 'x' FROM 'xxxbarxxx');</code></strong>
-> 'bar'
mysql> <strong class="userinput"><code>SELECT TRIM(TRAILING 'xyz' FROM 'barxxyz');</code></strong>
-> 'barx'
</pre><p>
This function is multi-byte safe.
</p></li><li><p><a id="function_ucase"></a>
<a id="id2824566" class="indexterm"></a>
<code class="literal">UCASE(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
<code class="literal">UCASE()</code> is a synonym for
<code class="literal">UPPER()</code>.
</p></li><li><p><a id="function_unhex"></a>
<a id="id2824634" class="indexterm"></a>
<code class="literal">UNHEX(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
Performs the inverse operation of
<code class="literal">HEX(<em class="replaceable"><code>str</code></em>)</code>. That
is, it interprets each pair of hexadecimal digits in the
argument as a number and converts it to the character
represented by the number. The resulting characters are
returned as a binary string.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT UNHEX('4D7953514C');</code></strong>
-> 'MySQL'
mysql> <strong class="userinput"><code>SELECT 0x4D7953514C;</code></strong>
-> 'MySQL'
mysql> <strong class="userinput"><code>SELECT UNHEX(HEX('string'));</code></strong>
-> 'string'
mysql> <strong class="userinput"><code>SELECT HEX(UNHEX('1267'));</code></strong>
-> '1267'
</pre><p>
The characters in the argument string must be legal
hexadecimal digits: <code class="literal">'0'</code> ..
<code class="literal">'9'</code>, <code class="literal">'A'</code> ..
<code class="literal">'F'</code>, <code class="literal">'a'</code> ..
<code class="literal">'f'</code>. If <code class="literal">UNHEX()</code>
encounters any non-hexadecimal digits in the argument, it
returns <code class="literal">NULL</code>:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT UNHEX('GG');</code></strong>
+-------------+
| UNHEX('GG') |
+-------------+
| NULL |
+-------------+
</pre><p>
A <code class="literal">NULL</code> result can occur if the argument to
<code class="literal">UNHEX()</code> is a <code class="literal">BINARY</code>
column, because values are padded with 0x00 bytes when stored
but those bytes are not stripped on retrieval. For example
<code class="literal">'aa'</code> is stored into a
<code class="literal">CHAR(3)</code> column as
<code class="literal">'aa '</code> and retrieved as
<code class="literal">'aa'</code> (with the trailing pad space
stripped), so <code class="literal">UNHEX()</code> for the column value
returns <code class="literal">'A'</code>. By contrast
<code class="literal">'aa'</code> is stored into a
<code class="literal">BINARY(3)</code> column as
<code class="literal">'aa\0'</code> and retrieved as
<code class="literal">'aa\0'</code> (with the trailing pad
<code class="literal">0x00</code> byte not stripped).
<code class="literal">'\0'</code> is not a legal hexadecimal digit, so
<code class="literal">UNHEX()</code> for the column value returns
<code class="literal">NULL</code>.
</p></li><li><p><a id="function_upper"></a>
<a id="id2824834" class="indexterm"></a>
<code class="literal">UPPER(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
Returns the string <em class="replaceable"><code>str</code></em> with all
characters changed to uppercase according to the current
character set mapping. The default is
<code class="literal">latin1</code> (cp1252 West European).
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT UPPER('Hej');</code></strong>
-> 'HEJ'
</pre><p>
This function is multi-byte safe.
</p></li></ul></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="string-comparison-functions"></a>String Comparison Functions</h3></div></div></div><a id="id2824903" class="indexterm"></a><a id="id2824910" class="indexterm"></a><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><thead><tr><th><span class="bold"><strong>Name</strong></span></th><th><span class="bold"><strong>Description</strong></span></th></tr></thead><tbody><tr><td><a href="mysqlqb_functions.html#operator_equal-to"><code class="literal"><=></code></a></td><td>NULL-safe equal to operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_equal"><code class="literal">=</code></a></td><td>Equal operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_greater-than-or-equal"><code class="literal">>=</code></a></td><td>Greater than or equal operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_greater-than"><code class="literal">></code></a></td><td>Greater than operator</td></tr><tr><td><a href="mysqlqb_functions.html#function_is-null"><code class="literal">IS NULL</code></a></td><td>NULL value test</td></tr><tr><td><a href="mysqlqb_functions.html#operator_is"><code class="literal">IS</code></a></td><td>Test a value against a boolean</td></tr><tr><td><a href="mysqlqb_functions.html#operator_less-than-or-equal"><code class="literal"><=</code></a></td><td>Less than or equal operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_less-than"><code class="literal"><</code></a></td><td>Less than operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_like"><code class="literal">LIKE</code></a></td><td>Simple pattern matching</td></tr><tr><td><a href="mysqlqb_functions.html#operator_not-equal"><code class="literal">!=</code>, <code class="literal"><></code></a></td><td>Not equal operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_not-like"><code class="literal">NOT LIKE</code></a></td><td>Negation of simple pattern matching</td></tr><tr><td><a href="mysqlqb_functions.html#operator_sounds-like"><code class="literal">SOUNDS LIKE</code></a></td><td>Compare sounds</td></tr></tbody></table></div><p>
If a string function is given a binary string as an argument,
the resulting string is also a binary string. A number converted
to a string is treated as a binary string. This affects only
comparisons.
</p><a id="id2825106" class="indexterm"></a><a id="id2825116" class="indexterm"></a><p>
Normally, if any expression in a string comparison is case
sensitive, the comparison is performed in case-sensitive
fashion.
</p><div class="itemizedlist"><ul type="disc"><li><p><a id="operator_like"></a>
<a id="id2825159" class="indexterm"></a>
<code class="literal"><em class="replaceable"><code>expr</code></em> LIKE
<em class="replaceable"><code>pat</code></em> [ESCAPE
'<em class="replaceable"><code>escape_char</code></em>']</code>
</p><p>
Pattern matching using SQL simple regular expression
comparison. Returns <code class="literal">1</code>
(<code class="literal">TRUE</code>) or <code class="literal">0</code>
(<code class="literal">FALSE</code>). If either
<em class="replaceable"><code>expr</code></em> or
<em class="replaceable"><code>pat</code></em> is <code class="literal">NULL</code>,
the result is <code class="literal">NULL</code>.
</p><p>
The pattern need not be a literal string. For example, it
can be specified as a string expression or table column.
</p><p>
Per the SQL standard, <code class="literal">LIKE</code> performs
matching on a per-character basis, thus it can produce
results different from the <code class="literal">=</code> comparison
operator:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 'ä' LIKE 'ae' COLLATE latin1_german2_ci;</code></strong>
+-----------------------------------------+
| 'ä' LIKE 'ae' COLLATE latin1_german2_ci |
+-----------------------------------------+
| 0 |
+-----------------------------------------+
mysql> <strong class="userinput"><code>SELECT 'ä' = 'ae' COLLATE latin1_german2_ci;</code></strong>
+--------------------------------------+
| 'ä' = 'ae' COLLATE latin1_german2_ci |
+--------------------------------------+
| 1 |
+--------------------------------------+
</pre><p>
With <code class="literal">LIKE</code> you can use the following two
wildcard characters in the pattern:
</p><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><tbody><tr><td><span class="bold"><strong>Character</strong></span></td><td><span class="bold"><strong>Description</strong></span></td></tr><tr><td><code class="literal">%</code></td><td>Matches any number of characters, even zero characters</td></tr><tr><td><code class="literal">_</code></td><td>Matches exactly one character</td></tr></tbody></table></div><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 'David!' LIKE 'David_';</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT 'David!' LIKE '%D%v%';</code></strong>
-> 1
</pre><p>
To test for literal instances of a wildcard character,
precede it by the escape character. If you do not specify
the <code class="literal">ESCAPE</code> character,
‘<code class="literal">\</code>’ is assumed.
</p><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><tbody><tr><td><span class="bold"><strong>String</strong></span></td><td><span class="bold"><strong>Description</strong></span></td></tr><tr><td><code class="literal">\%</code></td><td>Matches one ‘<code class="literal">%</code>’ character</td></tr><tr><td><code class="literal">\_</code></td><td>Matches one ‘<code class="literal">_</code>’ character</td></tr></tbody></table></div><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 'David!' LIKE 'David\_';</code></strong>
-> 0
mysql> <strong class="userinput"><code>SELECT 'David_' LIKE 'David\_';</code></strong>
-> 1
</pre><p>
To specify a different escape character, use the
<code class="literal">ESCAPE</code> clause:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 'David_' LIKE 'David|_' ESCAPE '|';</code></strong>
-> 1
</pre><p>
The escape sequence should be empty or one character long.
As of MySQL 5.0.16, if the
<code class="literal">NO_BACKSLASH_ESCAPES</code> SQL mode is enabled,
the sequence cannot be empty.
</p><p>
The following two statements illustrate that string
comparisons are not case sensitive unless one of the
operands is a binary string:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 'abc' LIKE 'ABC';</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT 'abc' LIKE BINARY 'ABC';</code></strong>
-> 0
</pre><p>
In MySQL, <code class="literal">LIKE</code> is allowed on numeric
expressions. (This is an extension to the standard SQL
<code class="literal">LIKE</code>.)
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 10 LIKE '1%';</code></strong>
-> 1
</pre><p>
<span class="bold"><strong>Note</strong></span>: Because MySQL uses C
escape syntax in strings (for example,
‘<code class="literal">\n</code>’ to represent a newline
character), you must double any
‘<code class="literal">\</code>’ that you use in
<code class="literal">LIKE</code> strings. For example, to search for
‘<code class="literal">\n</code>’, specify it as
‘<code class="literal">\\n</code>’. To search for
‘<code class="literal">\</code>’, specify it as
‘<code class="literal">\\\\</code>’; this is because the
backslashes are stripped once by the parser and again when
the pattern match is made, leaving a single backslash to be
matched against. (Exception: At the end of the pattern
string, backslash can be specified as
‘<code class="literal">\\</code>’. At the end of the
string, backslash stands for itself because there is nothing
following to escape.)
</p></li><li><p><a id="operator_not-like"></a>
<a id="id2825593" class="indexterm"></a>
<code class="literal"><em class="replaceable"><code>expr</code></em> NOT LIKE
<em class="replaceable"><code>pat</code></em> [ESCAPE
'<em class="replaceable"><code>escape_char</code></em>']</code>
</p><p>
This is the same as <code class="literal">NOT
(<em class="replaceable"><code>expr</code></em> LIKE
<em class="replaceable"><code>pat</code></em> [ESCAPE
'<em class="replaceable"><code>escape_char</code></em>'])</code>.
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
Aggegate queries involving <code class="literal">NOT LIKE</code>
comparisons with columns containing
<code class="literal">NULL</code> may yield unexpected results. For
example, consider the following table and data:
</p><pre class="programlisting">CREATE TABLE foo (bar VARCHAR(10));
INSERT INTO foo VALUES (NULL), (NULL);
</pre><p>
The query <code class="literal">SELECT COUNT(*) FROM foo WHERE bar LIKE
'%baz%';</code> returns <code class="literal">0</code>. You might
assume that <code class="literal">SELECT COUNT(*) FROM foo WHERE bar
NOT LIKE '%baz%';</code> would return
<code class="literal">2</code>. However, this is not the case: The
second query returns <code class="literal">0</code>. This is because
<code class="literal">NULL NOT LIKE
<em class="replaceable"><code>expr</code></em></code> always returns
<code class="literal">NULL</code>, regardless of the value of
<em class="replaceable"><code>expr</code></em>. The same is true for
aggregate queries involving <code class="literal">NULL</code> and
comparisons using <code class="literal">NOT RLIKE</code> or
<code class="literal">NOT REGEXP</code>. In such cases, you must
test explicitly for <code class="literal">NOT NULL</code> using
<code class="literal">OR</code> (and not <code class="literal">AND</code>), as
shown here:
</p><pre class="programlisting">SELECT COUNT(*) FROM foo WHERE bar NOT LIKE '%baz%' OR bar IS NULL;
</pre><p>
</p></div></li><li><p><a id="operator_not-regexp"></a>
<a id="id2825755" class="indexterm"></a>
<code class="literal"><em class="replaceable"><code>expr</code></em> NOT REGEXP
<em class="replaceable"><code>pat</code></em></code>,
<code class="literal"><em class="replaceable"><code>expr</code></em> NOT RLIKE
<em class="replaceable"><code>pat</code></em></code>
</p><p>
This is the same as <code class="literal">NOT
(<em class="replaceable"><code>expr</code></em> REGEXP
<em class="replaceable"><code>pat</code></em>)</code>.
</p></li><li><p><a id="operator_regexp"></a>
<a id="id2825834" class="indexterm"></a>
<a id="id2825840" class="indexterm"></a>
<a id="id2825851" class="indexterm"></a>
<a id="id2825857" class="indexterm"></a>
<code class="literal"><em class="replaceable"><code>expr</code></em> REGEXP
<em class="replaceable"><code>pat</code></em></code>,
<code class="literal"><em class="replaceable"><code>expr</code></em> RLIKE
<em class="replaceable"><code>pat</code></em></code>
</p><p>
Performs a pattern match of a string expression
<em class="replaceable"><code>expr</code></em> against a pattern
<em class="replaceable"><code>pat</code></em>. The pattern can be an
extended regular expression. The syntax for regular
expressions is discussed in <a href="mysqlqb_functions.html#regexp" title="Regular Expressions">Regular Expressions</a>.
Returns <code class="literal">1</code> if
<em class="replaceable"><code>expr</code></em> matches
<em class="replaceable"><code>pat</code></em>; otherwise it returns
<code class="literal">0</code>. If either
<em class="replaceable"><code>expr</code></em> or
<em class="replaceable"><code>pat</code></em> is <code class="literal">NULL</code>,
the result is <code class="literal">NULL</code>.
<code class="literal">RLIKE</code> is a synonym for
<code class="literal">REGEXP</code>, provided for
<code class="literal">mSQL</code> compatibility.
</p><p>
The pattern need not be a literal string. For example, it
can be specified as a string expression or table column.
</p><p>
<span class="bold"><strong>Note</strong></span>: Because MySQL uses
the C escape syntax in strings (for example,
‘<code class="literal">\n</code>’ to represent the newline
character), you must double any
‘<code class="literal">\</code>’ that you use in your
<code class="literal">REGEXP</code> strings.
</p><p>
<code class="literal">REGEXP</code> is not case sensitive, except when
used with binary strings.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 'Monty!' REGEXP 'm%y%%';</code></strong>
-> 0
mysql> <strong class="userinput"><code>SELECT 'Monty!' REGEXP '.*';</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT 'new*\n*line' REGEXP 'new\\*.\\*line';</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT 'a' REGEXP 'A', 'a' REGEXP BINARY 'A';</code></strong>
-> 1 0
mysql> <strong class="userinput"><code>SELECT 'a' REGEXP '^[a-d]';</code></strong>
-> 1
</pre><p>
<code class="literal">REGEXP</code> and <code class="literal">RLIKE</code> use
the current character set when deciding the type of a
character. The default is <code class="literal">latin1</code> (cp1252
West European). <span class="bold"><strong>Warning</strong></span>:
These operators are not multi-byte safe.
</p></li><li><p><a id="function_strcmp"></a>
<a id="id2826070" class="indexterm"></a>
<code class="literal">STRCMP(<em class="replaceable"><code>expr1</code></em>,<em class="replaceable"><code>expr2</code></em>)</code>
</p><p>
<code class="literal">STRCMP()</code> returns <code class="literal">0</code> if
the strings are the same, <code class="literal">-1</code> if the first
argument is smaller than the second according to the current
sort order, and <code class="literal">1</code> otherwise.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT STRCMP('text', 'text2');</code></strong>
-> -1
mysql> <strong class="userinput"><code>SELECT STRCMP('text2', 'text');</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT STRCMP('text', 'text');</code></strong>
-> 0
</pre><p>
<code class="literal">STRCMP()</code> uses the current character set
when performing comparisons. This makes the default
comparison behavior case insensitive unless one or both of
the operands are binary strings.
</p></li></ul></div></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="regexp"></a>Regular Expressions</h3></div></div></div><a id="id2826165" class="indexterm"></a><a id="id2826172" class="indexterm"></a><a id="id2826178" class="indexterm"></a><a id="id2826185" class="indexterm"></a><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><thead><tr><th><span class="bold"><strong>Name</strong></span></th><th><span class="bold"><strong>Description</strong></span></th></tr></thead><tbody><tr><td><a href="mysqlqb_functions.html#operator_not-regexp"><code class="literal">NOT RGEXP</code></a></td><td>Negation of REGEXP</td></tr><tr><td><a href="mysqlqb_functions.html#operator_regexp"><code class="literal">REGEXP</code></a></td><td>Pattern matching using regular expressions</td></tr><tr><td><a href="mysqlqb_functions.html#operator_regexp"><code class="literal">RLIKE</code></a></td><td>Synonym for REGEXP</td></tr></tbody></table></div><p>
A regular expression is a powerful way of specifying a pattern
for a complex search.
</p><p>
MySQL uses Henry Spencer's implementation of regular
expressions, which is aimed at conformance with POSIX 1003.2.
See <a href="http://dev.mysql.com/doc/refman/5.1/en/credits.html" target="_top">Credits</a>. MySQL uses the extended version
to support pattern-matching operations performed with the
<code class="literal">REGEXP</code> operator in SQL statements. See
<a href="http://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html" target="_top">Pattern Matching</a>, and
<a href="mysqlqb_functions.html#string-comparison-functions" title="String Comparison Functions">String Comparison Functions</a>.
</p><p>
This section is a summary, with examples, of the special
characters and constructs that can be used in MySQL for
<code class="literal">REGEXP</code> operations. It does not contain all
the details that can be found in Henry Spencer's
<code class="literal">regex(7)</code> manual page. That manual page is
included in MySQL source distributions, in the
<code class="filename">regex.7</code> file under the
<code class="filename">regex</code> directory.
</p><p>
A regular expression describes a set of strings. The simplest
regular expression is one that has no special characters in it.
For example, the regular expression <code class="literal">hello</code>
matches <code class="literal">hello</code> and nothing else.
</p><p>
Non-trivial regular expressions use certain special constructs
so that they can match more than one string. For example, the
regular expression <code class="literal">hello|word</code> matches either
the string <code class="literal">hello</code> or the string
<code class="literal">word</code>.
</p><p>
As a more complex example, the regular expression
<code class="literal">B[an]*s</code> matches any of the strings
<code class="literal">Bananas</code>, <code class="literal">Baaaaas</code>,
<code class="literal">Bs</code>, and any other string starting with a
<code class="literal">B</code>, ending with an <code class="literal">s</code>, and
containing any number of <code class="literal">a</code> or
<code class="literal">n</code> characters in between.
</p><p>
A regular expression for the <code class="literal">REGEXP</code> operator
may use any of the following special characters and constructs:
</p><div class="itemizedlist"><ul type="disc"><li><p>
<code class="literal">^</code>
</p><p>
Match the beginning of a string.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 'fo\nfo' REGEXP '^fo$';</code></strong> -> 0
mysql> <strong class="userinput"><code>SELECT 'fofo' REGEXP '^fo';</code></strong> -> 1
</pre></li><li><p>
<code class="literal">$</code>
</p><p>
Match the end of a string.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 'fo\no' REGEXP '^fo\no$';</code></strong> -> 1
mysql> <strong class="userinput"><code>SELECT 'fo\no' REGEXP '^fo$';</code></strong> -> 0
</pre></li><li><p>
<code class="literal">.</code>
</p><p>
Match any character (including carriage return and newline).
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 'fofo' REGEXP '^f.*$';</code></strong> -> 1
mysql> <strong class="userinput"><code>SELECT 'fo\r\nfo' REGEXP '^f.*$';</code></strong> -> 1
</pre></li><li><p>
<code class="literal">a*</code>
</p><p>
Match any sequence of zero or more <code class="literal">a</code>
characters.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 'Ban' REGEXP '^Ba*n';</code></strong> -> 1
mysql> <strong class="userinput"><code>SELECT 'Baaan' REGEXP '^Ba*n';</code></strong> -> 1
mysql> <strong class="userinput"><code>SELECT 'Bn' REGEXP '^Ba*n';</code></strong> -> 1
</pre></li><li><p>
<code class="literal">a+</code>
</p><p>
Match any sequence of one or more <code class="literal">a</code>
characters.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 'Ban' REGEXP '^Ba+n';</code></strong> -> 1
mysql> <strong class="userinput"><code>SELECT 'Bn' REGEXP '^Ba+n';</code></strong> -> 0
</pre></li><li><p>
<code class="literal">a?</code>
</p><p>
Match either zero or one <code class="literal">a</code> character.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 'Bn' REGEXP '^Ba?n';</code></strong> -> 1
mysql> <strong class="userinput"><code>SELECT 'Ban' REGEXP '^Ba?n';</code></strong> -> 1
mysql> <strong class="userinput"><code>SELECT 'Baan' REGEXP '^Ba?n';</code></strong> -> 0
</pre></li><li><p>
<code class="literal">de|abc</code>
</p><p>
Match either of the sequences <code class="literal">de</code> or
<code class="literal">abc</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 'pi' REGEXP 'pi|apa';</code></strong> -> 1
mysql> <strong class="userinput"><code>SELECT 'axe' REGEXP 'pi|apa';</code></strong> -> 0
mysql> <strong class="userinput"><code>SELECT 'apa' REGEXP 'pi|apa';</code></strong> -> 1
mysql> <strong class="userinput"><code>SELECT 'apa' REGEXP '^(pi|apa)$';</code></strong> -> 1
mysql> <strong class="userinput"><code>SELECT 'pi' REGEXP '^(pi|apa)$';</code></strong> -> 1
mysql> <strong class="userinput"><code>SELECT 'pix' REGEXP '^(pi|apa)$';</code></strong> -> 0
</pre></li><li><p>
<code class="literal">(abc)*</code>
</p><p>
Match zero or more instances of the sequence
<code class="literal">abc</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 'pi' REGEXP '^(pi)*$';</code></strong> -> 1
mysql> <strong class="userinput"><code>SELECT 'pip' REGEXP '^(pi)*$';</code></strong> -> 0
mysql> <strong class="userinput"><code>SELECT 'pipi' REGEXP '^(pi)*$';</code></strong> -> 1
</pre></li><li><p>
<code class="literal">{1}</code>, <code class="literal">{2,3}</code>
</p><p>
<code class="literal">{n}</code> or <code class="literal">{m,n}</code> notation
provides a more general way of writing regular expressions
that match many occurrences of the previous atom (or
“<span class="quote">piece</span>”) of the pattern. <code class="literal">m</code>
and <code class="literal">n</code> are integers.
</p><div class="itemizedlist"><ul type="circle"><li><p>
<code class="literal">a*</code>
</p><p>
Can be written as <code class="literal">a{0,}</code>.
</p></li><li><p>
<code class="literal">a+</code>
</p><p>
Can be written as <code class="literal">a{1,}</code>.
</p></li><li><p>
<code class="literal">a?</code>
</p><p>
Can be written as <code class="literal">a{0,1}</code>.
</p></li></ul></div><p>
To be more precise, <code class="literal">a{n}</code> matches exactly
<code class="literal">n</code> instances of <code class="literal">a</code>.
<code class="literal">a{n,}</code> matches <code class="literal">n</code> or
more instances of <code class="literal">a</code>.
<code class="literal">a{m,n}</code> matches <code class="literal">m</code>
through <code class="literal">n</code> instances of
<code class="literal">a</code>, inclusive.
</p><p>
<code class="literal">m</code> and <code class="literal">n</code> must be in the
range from <code class="literal">0</code> to
<code class="literal">RE_DUP_MAX</code> (default 255), inclusive. If
both <code class="literal">m</code> and <code class="literal">n</code> are
given, <code class="literal">m</code> must be less than or equal to
<code class="literal">n</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 'abcde' REGEXP 'a[bcd]{2}e';</code></strong> -> 0
mysql> <strong class="userinput"><code>SELECT 'abcde' REGEXP 'a[bcd]{3}e';</code></strong> -> 1
mysql> <strong class="userinput"><code>SELECT 'abcde' REGEXP 'a[bcd]{1,10}e';</code></strong> -> 1
</pre></li><li><p>
<code class="literal">[a-dX]</code>, <code class="literal">[^a-dX]</code>
</p><p>
Matches any character that is (or is not, if ^ is used)
either <code class="literal">a</code>, <code class="literal">b</code>,
<code class="literal">c</code>, <code class="literal">d</code> or
<code class="literal">X</code>. A <code class="literal">-</code> character
between two other characters forms a range that matches all
characters from the first character to the second. For
example, <code class="literal">[0-9]</code> matches any decimal digit.
To include a literal <code class="literal">]</code> character, it must
immediately follow the opening bracket <code class="literal">[</code>.
To include a literal <code class="literal">-</code> character, it must
be written first or last. Any character that does not have a
defined special meaning inside a <code class="literal">[]</code> pair
matches only itself.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 'aXbc' REGEXP '[a-dXYZ]';</code></strong> -> 1
mysql> <strong class="userinput"><code>SELECT 'aXbc' REGEXP '^[a-dXYZ]$';</code></strong> -> 0
mysql> <strong class="userinput"><code>SELECT 'aXbc' REGEXP '^[a-dXYZ]+$';</code></strong> -> 1
mysql> <strong class="userinput"><code>SELECT 'aXbc' REGEXP '^[^a-dXYZ]+$';</code></strong> -> 0
mysql> <strong class="userinput"><code>SELECT 'gheis' REGEXP '^[^a-dXYZ]+$';</code></strong> -> 1
mysql> <strong class="userinput"><code>SELECT 'gheisa' REGEXP '^[^a-dXYZ]+$';</code></strong> -> 0
</pre></li><li><p>
<code class="literal">[.characters.]</code>
</p><p>
Within a bracket expression (written using
<code class="literal">[</code> and <code class="literal">]</code>), matches the
sequence of characters of that collating element.
<code class="literal">characters</code> is either a single character
or a character name like <code class="literal">newline</code>. The
following table lists the allowable character names.
</p><p>
The following table shows the allowable character names and
the characters that they match. For characters given as
numeric values, the values are represented in octal.
</p><div class="informaltable"><table border="1"><colgroup><col /><col /><col /><col /></colgroup><tbody><tr><td><span class="bold"><strong>Name</strong></span></td><td><span class="bold"><strong>Character</strong></span></td><td><span class="bold"><strong>Name</strong></span></td><td><span class="bold"><strong>Character</strong></span></td></tr><tr><td><code class="literal">NUL</code></td><td><code class="literal">0</code></td><td><code class="literal">SOH</code></td><td><code class="literal">001</code></td></tr><tr><td><code class="literal">STX</code></td><td><code class="literal">002</code></td><td><code class="literal">ETX</code></td><td><code class="literal">003</code></td></tr><tr><td><code class="literal">EOT</code></td><td><code class="literal">004</code></td><td><code class="literal">ENQ</code></td><td><code class="literal">005</code></td></tr><tr><td><code class="literal">ACK</code></td><td><code class="literal">006</code></td><td><code class="literal">BEL</code></td><td><code class="literal">007</code></td></tr><tr><td><code class="literal">alert</code></td><td><code class="literal">007</code></td><td><code class="literal">BS</code></td><td><code class="literal">010</code></td></tr><tr><td><code class="literal">backspace</code></td><td><code class="literal">'\b'</code></td><td><code class="literal">HT</code></td><td><code class="literal">011</code></td></tr><tr><td><code class="literal">tab</code></td><td><code class="literal">'\t'</code></td><td><code class="literal">LF</code></td><td><code class="literal">012</code></td></tr><tr><td><code class="literal">newline</code></td><td><code class="literal">'\n'</code></td><td><code class="literal">VT</code></td><td><code class="literal">013</code></td></tr><tr><td><code class="literal">vertical-tab</code></td><td><code class="literal">'\v'</code></td><td><code class="literal">FF</code></td><td><code class="literal">014</code></td></tr><tr><td><code class="literal">form-feed</code></td><td><code class="literal">'\f'</code></td><td><code class="literal">CR</code></td><td><code class="literal">015</code></td></tr><tr><td><code class="literal">carriage-return</code></td><td><code class="literal">'\r'</code></td><td><code class="literal">SO</code></td><td><code class="literal">016</code></td></tr><tr><td><code class="literal">SI</code></td><td><code class="literal">017</code></td><td><code class="literal">DLE</code></td><td><code class="literal">020</code></td></tr><tr><td><code class="literal">DC1</code></td><td><code class="literal">021</code></td><td><code class="literal">DC2</code></td><td><code class="literal">022</code></td></tr><tr><td><code class="literal">DC3</code></td><td><code class="literal">023</code></td><td><code class="literal">DC4</code></td><td><code class="literal">024</code></td></tr><tr><td><code class="literal">NAK</code></td><td><code class="literal">025</code></td><td><code class="literal">SYN</code></td><td><code class="literal">026</code></td></tr><tr><td><code class="literal">ETB</code></td><td><code class="literal">027</code></td><td><code class="literal">CAN</code></td><td><code class="literal">030</code></td></tr><tr><td><code class="literal">EM</code></td><td><code class="literal">031</code></td><td><code class="literal">SUB</code></td><td><code class="literal">032</code></td></tr><tr><td><code class="literal">ESC</code></td><td><code class="literal">033</code></td><td><code class="literal">IS4</code></td><td><code class="literal">034</code></td></tr><tr><td><code class="literal">FS</code></td><td><code class="literal">034</code></td><td><code class="literal">IS3</code></td><td><code class="literal">035</code></td></tr><tr><td><code class="literal">GS</code></td><td><code class="literal">035</code></td><td><code class="literal">IS2</code></td><td><code class="literal">036</code></td></tr><tr><td><code class="literal">RS</code></td><td><code class="literal">036</code></td><td><code class="literal">IS1</code></td><td><code class="literal">037</code></td></tr><tr><td><code class="literal">US</code></td><td><code class="literal">037</code></td><td><code class="literal">space</code></td><td><code class="literal">' '</code></td></tr><tr><td><code class="literal">exclamation-mark</code></td><td><code class="literal">'!'</code></td><td><code class="literal">quotation-mark</code></td><td><code class="literal">'"'</code></td></tr><tr><td><code class="literal">number-sign</code></td><td><code class="literal">'#'</code></td><td><code class="literal">dollar-sign</code></td><td><code class="literal">'$'</code></td></tr><tr><td><code class="literal">percent-sign</code></td><td><code class="literal">'%'</code></td><td><code class="literal">ampersand</code></td><td><code class="literal">'&'</code></td></tr><tr><td><code class="literal">apostrophe</code></td><td><code class="literal">'\''</code></td><td><code class="literal">left-parenthesis</code></td><td><code class="literal">'('</code></td></tr><tr><td><code class="literal">right-parenthesis</code></td><td><code class="literal">')'</code></td><td><code class="literal">asterisk</code></td><td><code class="literal">'*'</code></td></tr><tr><td><code class="literal">plus-sign</code></td><td><code class="literal">'+'</code></td><td><code class="literal">comma</code></td><td><code class="literal">','</code></td></tr><tr><td><code class="literal">hyphen</code></td><td><code class="literal">'-'</code></td><td><code class="literal">hyphen-minus</code></td><td><code class="literal">'-'</code></td></tr><tr><td><code class="literal">period</code></td><td><code class="literal">'.'</code></td><td><code class="literal">full-stop</code></td><td><code class="literal">'.'</code></td></tr><tr><td><code class="literal">slash</code></td><td><code class="literal">'/'</code></td><td><code class="literal">solidus</code></td><td><code class="literal">'/'</code></td></tr><tr><td><code class="literal">zero</code></td><td><code class="literal">'0'</code></td><td><code class="literal">one</code></td><td><code class="literal">'1'</code></td></tr><tr><td><code class="literal">two</code></td><td><code class="literal">'2'</code></td><td><code class="literal">three</code></td><td><code class="literal">'3'</code></td></tr><tr><td><code class="literal">four</code></td><td><code class="literal">'4'</code></td><td><code class="literal">five</code></td><td><code class="literal">'5'</code></td></tr><tr><td><code class="literal">six</code></td><td><code class="literal">'6'</code></td><td><code class="literal">seven</code></td><td><code class="literal">'7'</code></td></tr><tr><td><code class="literal">eight</code></td><td><code class="literal">'8'</code></td><td><code class="literal">nine</code></td><td><code class="literal">'9'</code></td></tr><tr><td><code class="literal">colon</code></td><td><code class="literal">':'</code></td><td><code class="literal">semicolon</code></td><td><code class="literal">';'</code></td></tr><tr><td><code class="literal">less-than-sign</code></td><td><code class="literal">'<'</code></td><td><code class="literal">equals-sign</code></td><td><code class="literal">'='</code></td></tr><tr><td><code class="literal">greater-than-sign</code></td><td><code class="literal">'>'</code></td><td><code class="literal">question-mark</code></td><td><code class="literal">'?'</code></td></tr><tr><td><code class="literal">commercial-at</code></td><td><code class="literal">'@'</code></td><td><code class="literal">left-square-bracket</code></td><td><code class="literal">'['</code></td></tr><tr><td><code class="literal">backslash</code></td><td><code class="literal">'\\'</code></td><td><code class="literal">reverse-solidus</code></td><td><code class="literal">'\\'</code></td></tr><tr><td><code class="literal">right-square-bracket</code></td><td><code class="literal">']'</code></td><td><code class="literal">circumflex</code></td><td><code class="literal">'^'</code></td></tr><tr><td><code class="literal">circumflex-accent</code></td><td><code class="literal">'^'</code></td><td><code class="literal">underscore</code></td><td><code class="literal">'_'</code></td></tr><tr><td><code class="literal">low-line</code></td><td><code class="literal">'_'</code></td><td><code class="literal">grave-accent</code></td><td><code class="literal">'`'</code></td></tr><tr><td><code class="literal">left-brace</code></td><td><code class="literal">'{'</code></td><td><code class="literal">left-curly-bracket</code></td><td><code class="literal">'{'</code></td></tr><tr><td><code class="literal">vertical-line</code></td><td><code class="literal">'|'</code></td><td><code class="literal">right-brace</code></td><td><code class="literal">'}'</code></td></tr><tr><td><code class="literal">right-curly-bracket</code></td><td><code class="literal">'}'</code></td><td><code class="literal">tilde</code></td><td><code class="literal">'~'</code></td></tr><tr><td><code class="literal">DEL</code></td><td><code class="literal">177</code></td><td> </td><td> </td></tr></tbody></table></div><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT '~' REGEXP '[[.~.]]';</code></strong> -> 1
mysql> <strong class="userinput"><code>SELECT '~' REGEXP '[[.tilde.]]';</code></strong> -> 1
</pre></li><li><p>
<code class="literal">[=character_class=]</code>
</p><p>
Within a bracket expression (written using
<code class="literal">[</code> and <code class="literal">]</code>),
<code class="literal">[=character_class=]</code> represents an
equivalence class. It matches all characters with the same
collation value, including itself. For example, if
<code class="literal">o</code> and <code class="literal">(+)</code> are the
members of an equivalence class, then
<code class="literal">[[=o=]]</code>, <code class="literal">[[=(+)=]]</code>,
and <code class="literal">[o(+)]</code> are all synonymous. An
equivalence class may not be used as an endpoint of a range.
</p></li><li><p>
<code class="literal">[:character_class:]</code>
</p><p>
Within a bracket expression (written using
<code class="literal">[</code> and <code class="literal">]</code>),
<code class="literal">[:character_class:]</code> represents a
character class that matches all characters belonging to
that class. The following table lists the standard class
names. These names stand for the character classes defined
in the <code class="literal">ctype(3)</code> manual page. A particular
locale may provide other class names. A character class may
not be used as an endpoint of a range.
</p><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><tbody><tr><td><code class="literal">alnum</code></td><td>Alphanumeric characters</td></tr><tr><td><code class="literal">alpha</code></td><td>Alphabetic characters</td></tr><tr><td><code class="literal">blank</code></td><td>Whitespace characters</td></tr><tr><td><code class="literal">cntrl</code></td><td>Control characters</td></tr><tr><td><code class="literal">digit</code></td><td>Digit characters</td></tr><tr><td><code class="literal">graph</code></td><td>Graphic characters</td></tr><tr><td><code class="literal">lower</code></td><td>Lowercase alphabetic characters</td></tr><tr><td><code class="literal">print</code></td><td>Graphic or space characters</td></tr><tr><td><code class="literal">punct</code></td><td>Punctuation characters</td></tr><tr><td><code class="literal">space</code></td><td>Space, tab, newline, and carriage return</td></tr><tr><td><code class="literal">upper</code></td><td>Uppercase alphabetic characters</td></tr><tr><td><code class="literal">xdigit</code></td><td>Hexadecimal digit characters</td></tr></tbody></table></div><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 'justalnums' REGEXP '[[:alnum:]]+';</code></strong> -> 1
mysql> <strong class="userinput"><code>SELECT '!!' REGEXP '[[:alnum:]]+';</code></strong> -> 0
</pre></li><li><p>
<code class="literal">[[:<:]]</code>, <code class="literal">[[:>:]]</code>
</p><p>
These markers stand for word boundaries. They match the
beginning and end of words, respectively. A word is a
sequence of word characters that is not preceded by or
followed by word characters. A word character is an
alphanumeric character in the <code class="literal">alnum</code> class
or an underscore (<code class="literal">_</code>).
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 'a word a' REGEXP '[[:<:]]word[[:>:]]';</code></strong> -> 1
mysql> <strong class="userinput"><code>SELECT 'a xword a' REGEXP '[[:<:]]word[[:>:]]';</code></strong> -> 0
</pre></li></ul></div><p>
To use a literal instance of a special character in a regular
expression, precede it by two backslash (\) characters. The
MySQL parser interprets one of the backslashes, and the regular
expression library interprets the other. For example, to match
the string <code class="literal">1+2</code> that contains the special
<code class="literal">+</code> character, only the last of the following
regular expressions is the correct one:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT '1+2' REGEXP '1+2';</code></strong> -> 0
mysql> <strong class="userinput"><code>SELECT '1+2' REGEXP '1\+2';</code></strong> -> 0
mysql> <strong class="userinput"><code>SELECT '1+2' REGEXP '1\\+2';</code></strong> -> 1
</pre></div></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="numeric-functions"></a>Numeric Functions</h2></div></div></div><div class="toc"><dl><dt><span class="section"><a href="mysqlqb_functions.html#arithmetic-functions">Arithmetic Operators</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#mathematical-functions">Mathematical Functions</a></span></dt></dl></div><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><thead><tr><th><span class="bold"><strong>Name</strong></span></th><th><span class="bold"><strong>Description</strong></span></th></tr></thead><tbody><tr><td><a href="mysqlqb_functions.html#function_abs"><code class="literal">ABS()</code></a></td><td>Return the absolute value</td></tr><tr><td><a href="mysqlqb_functions.html#function_acos"><code class="literal">ACOS()</code></a></td><td>Return the arc cosine</td></tr><tr><td><a href="mysqlqb_functions.html#function_asin"><code class="literal">ASIN()</code></a></td><td>Return the arc sine</td></tr><tr><td><a href="mysqlqb_functions.html#function_atan2"><code class="literal">ATAN2()</code>, <code class="literal">ATAN()</code></a></td><td>Return the arc tangent of the two arguments</td></tr><tr><td><a href="mysqlqb_functions.html#function_atan"><code class="literal">ATAN()</code></a></td><td>Return the arc tangent</td></tr><tr><td><a href="mysqlqb_functions.html#operator_by"><code class="literal">/</code></a></td><td>Division operator</td></tr><tr><td><a href="mysqlqb_functions.html#function_ceiling"><code class="literal">CEILING()</code>, <code class="literal">CEIL()</code></a></td><td>Return the smallest integer value not less than the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_cos"><code class="literal">COS()</code></a></td><td>Return the cosine</td></tr><tr><td><a href="mysqlqb_functions.html#function_cot"><code class="literal">COT()</code></a></td><td>Return the cotangent</td></tr><tr><td><a href="mysqlqb_functions.html#function_crc32"><code class="literal">CRC32()</code></a></td><td>Compute a cyclic redundancy check value</td></tr><tr><td><a href="mysqlqb_functions.html#function_degrees"><code class="literal">DEGREES()</code></a></td><td>Convert radians to degrees</td></tr><tr><td><a href="mysqlqb_functions.html#operator_div"><code class="literal">DIV</code></a></td><td>Integer division</td></tr><tr><td><a href="mysqlqb_functions.html#function_exp"><code class="literal">EXP()</code></a></td><td>Raise to the power of</td></tr><tr><td><a href="mysqlqb_functions.html#function_floor"><code class="literal">FLOOR()</code></a></td><td>Return the largest integer value not greater than the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_ln"><code class="literal">LN()</code></a></td><td>Return the natural logarithm of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_log10"><code class="literal">LOG10()</code></a></td><td>Return the base-10 logarithm of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_log2"><code class="literal">LOG2()</code></a></td><td>Return the base-2 logarithm of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_log"><code class="literal">LOG()</code></a></td><td> Return the natural logarithm of the first argument </td></tr><tr><td><a href="mysqlqb_functions.html#operator_minus"><code class="literal">-</code></a></td><td>Minus operator</td></tr><tr><td><a href="mysqlqb_functions.html#function_mod"><code class="literal">MOD()</code></a></td><td>Return the remainder</td></tr><tr><td><a href="mysqlqb_functions.html#operator_mod"><code class="literal">%</code></a></td><td>Modulo operator</td></tr><tr><td><a href="mysqlqb_functions.html#function_pi"><code class="literal">PI()</code></a></td><td>Return the value of pi</td></tr><tr><td><a href="mysqlqb_functions.html#operator_plus"><code class="literal">+</code></a></td><td>Addition operator</td></tr><tr><td><a href="mysqlqb_functions.html#function_pow"><code class="literal">POW()</code>, <code class="literal">POWER()</code></a></td><td>Return the argument raised to the specified power</td></tr><tr><td><a href="mysqlqb_functions.html#function_radians"><code class="literal">RADIANS()</code></a></td><td>Return argument converted to radians</td></tr><tr><td><a href="mysqlqb_functions.html#function_rand"><code class="literal">RAND()</code></a></td><td>Return a random floating-point value</td></tr><tr><td><a href="mysqlqb_functions.html#function_round"><code class="literal">ROUND()</code></a></td><td>Round the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_sign"><code class="literal">SIGN()</code></a></td><td>Return the sign of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_sin"><code class="literal">SIN()</code></a></td><td>Return the sine of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_sqrt"><code class="literal">SQRT()</code></a></td><td>Return the square root of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_tan"><code class="literal">TAN()</code></a></td><td>Return the tangent of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#operator_times"><code class="literal">*</code></a></td><td>Times operator</td></tr><tr><td><a href="mysqlqb_functions.html#function_truncate"><code class="literal">TRUNCATE()</code></a></td><td>Truncate to specified number of decimal places</td></tr><tr><td><a href="mysqlqb_functions.html#operator_unary-minus"><code class="literal">-</code></a></td><td>Change the sign of the argument</td></tr></tbody></table></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="arithmetic-functions"></a>Arithmetic Operators</h3></div></div></div><a id="id2828912" class="indexterm"></a><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><thead><tr><th><span class="bold"><strong>Name</strong></span></th><th><span class="bold"><strong>Description</strong></span></th></tr></thead><tbody><tr><td><a href="mysqlqb_functions.html#operator_by"><code class="literal">/</code></a></td><td>Division operator</td></tr><tr><td><a href="mysqlqb_functions.html#function_ceiling"><code class="literal">CEILING()</code>, <code class="literal">CEIL()</code></a></td><td>Return the smallest integer value not less than the argument</td></tr><tr><td><a href="mysqlqb_functions.html#operator_div"><code class="literal">DIV</code></a></td><td>Integer division</td></tr><tr><td><a href="mysqlqb_functions.html#operator_minus"><code class="literal">-</code></a></td><td>Minus operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_mod"><code class="literal">%</code></a></td><td>Modulo operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_plus"><code class="literal">+</code></a></td><td>Addition operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_times"><code class="literal">*</code></a></td><td>Times operator</td></tr><tr><td><a href="mysqlqb_functions.html#operator_unary-minus"><code class="literal">-</code></a></td><td>Change the sign of the argument</td></tr></tbody></table></div><p>
The usual arithmetic operators are available. The precision of
the result is determined according to the following rules:
</p><div class="itemizedlist"><ul type="disc"><li><p>
Note that in the case of <code class="literal">-</code>,
<code class="literal">+</code>, and <code class="literal">*</code>, the result
is calculated with <code class="literal">BIGINT</code> (64-bit)
precision if both arguments are integers.
</p></li><li><p>
If one of the arguments is an unsigned integer, and the
other argument is also an integer, the result is an unsigned
integer.
</p></li><li><p>
If any of the operands of a <code class="literal">+</code>,
<code class="literal">-</code>, <code class="literal">/</code>,
<code class="literal">*</code>, <code class="literal">%</code> is a real or
string value, then the precision of the result is the
precision of the argument with the maximum precision.
</p></li><li><p>
In multiplication and division, the precision of the result
when using two exact values is the precision of the first
argument + the value of the
<code class="literal">div_precision_increment</code> global variable.
For example, the expression <code class="literal">5.05 / 0.0014</code>
would have a precision of six decimal places
(<code class="literal">3607.142857</code>).
</p></li></ul></div><p>
These rules are applied for each operation, such that nested
calculations imply the precision of each component. Hence,
<code class="literal">(14620 / 9432456) / (24250 / 9432456)</code>, would
resolve first to <code class="literal">(0.0014) / (0.0026)</code>, with
the final result having 8 decimal places
(<code class="literal">0.57692308</code>).
</p><p>
Because of these rules and the method they are applied, care
should be taken to ensure that components and sub-components of
a calculation use the appropriate level of precision. See
<a href="mysqlqb_functions.html#cast-functions" title="Cast Functions and Operators">Cast Functions and Operators</a>.
</p><a id="id2829178" class="indexterm"></a><a id="id2829188" class="indexterm"></a><div class="itemizedlist"><ul type="disc"><li><p><a id="operator_plus"></a>
<a id="id2829215" class="indexterm"></a>
<a id="id2829222" class="indexterm"></a>
<code class="literal">+</code>
</p><p>
Addition:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 3+5;</code></strong>
-> 8
</pre></li><li><p><a id="operator_minus"></a>
<a id="id2829282" class="indexterm"></a>
<a id="id2829289" class="indexterm"></a>
<code class="literal">-</code>
</p><p>
Subtraction:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 3-5;</code></strong>
-> -2
</pre></li><li><p><a id="operator_unary-minus"></a>
<a id="id2829349" class="indexterm"></a>
<a id="id2829356" class="indexterm"></a>
<a id="id2829366" class="indexterm"></a>
<code class="literal">-</code>
</p><p>
Unary minus. This operator changes the sign of the argument.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT - 2;</code></strong>
-> -2
</pre><p>
<span class="bold"><strong>Note</strong></span>: If this operator is
used with a <code class="literal">BIGINT</code>, the return value is
also a <code class="literal">BIGINT</code>. This means that you should
avoid using <code class="literal">–</code> on integers that may
have the value of –2<sup>63</sup>.
</p></li><li><p><a id="operator_times"></a>
<a id="id2829454" class="indexterm"></a>
<a id="id2829461" class="indexterm"></a>
<code class="literal">*</code>
</p><p>
Multiplication:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 3*5;</code></strong>
-> 15
mysql> <strong class="userinput"><code>SELECT 18014398509481984*18014398509481984.0;</code></strong>
-> 324518553658426726783156020576256.0
mysql> <strong class="userinput"><code>SELECT 18014398509481984*18014398509481984;</code></strong>
-> 0
</pre><p>
The result of the last expression is incorrect because the
result of the integer multiplication exceeds the 64-bit
range of <code class="literal">BIGINT</code> calculations. (See
<a href="http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html" target="_top">Numeric Types</a>.)
</p></li><li><p><a id="operator_by"></a>
<a id="id2829549" class="indexterm"></a>
<a id="id2829556" class="indexterm"></a>
<code class="literal">/</code>
</p><p>
Division:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 3/5;</code></strong>
-> 0.60
</pre><p>
Division by zero produces a <code class="literal">NULL</code> result:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 102/(1-1);</code></strong>
-> NULL
</pre><p>
A division is calculated with <code class="literal">BIGINT</code>
arithmetic only if performed in a context where its result
is converted to an integer.
</p></li><li><p><a id="operator_div"></a>
<a id="id2829647" class="indexterm"></a>
<code class="literal">DIV</code>
</p><p>
Integer division. Similar to <code class="literal">FLOOR()</code>, but
is safe with <code class="literal">BIGINT</code> values.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 5 DIV 2;</code></strong>
-> 2
</pre></li><li><p><a id="operator_mod"></a>
<a id="id2829720" class="indexterm"></a>
<code class="literal"><em class="replaceable"><code>N</code></em> %
<em class="replaceable"><code>M</code></em></code>
</p><p>
Modulo operation. Returns the remainder of
<em class="replaceable"><code>N</code></em> divided by
<em class="replaceable"><code>M</code></em>. For more information, see the
description for the <code class="literal">MOD()</code> function in
<a href="mysqlqb_functions.html#mathematical-functions" title="Mathematical Functions">Mathematical Functions</a>.
</p></li></ul></div></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="mathematical-functions"></a>Mathematical Functions</h3></div></div></div><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><thead><tr><th><span class="bold"><strong>Name</strong></span></th><th><span class="bold"><strong>Description</strong></span></th></tr></thead><tbody><tr><td><a href="mysqlqb_functions.html#function_abs"><code class="literal">ABS()</code></a></td><td>Return the absolute value</td></tr><tr><td><a href="mysqlqb_functions.html#function_acos"><code class="literal">ACOS()</code></a></td><td>Return the arc cosine</td></tr><tr><td><a href="mysqlqb_functions.html#function_asin"><code class="literal">ASIN()</code></a></td><td>Return the arc sine</td></tr><tr><td><a href="mysqlqb_functions.html#function_atan2"><code class="literal">ATAN2()</code>, <code class="literal">ATAN()</code></a></td><td>Return the arc tangent of the two arguments</td></tr><tr><td><a href="mysqlqb_functions.html#function_atan"><code class="literal">ATAN()</code></a></td><td>Return the arc tangent</td></tr><tr><td><a href="mysqlqb_functions.html#function_cos"><code class="literal">COS()</code></a></td><td>Return the cosine</td></tr><tr><td><a href="mysqlqb_functions.html#function_cot"><code class="literal">COT()</code></a></td><td>Return the cotangent</td></tr><tr><td><a href="mysqlqb_functions.html#function_crc32"><code class="literal">CRC32()</code></a></td><td>Compute a cyclic redundancy check value</td></tr><tr><td><a href="mysqlqb_functions.html#function_degrees"><code class="literal">DEGREES()</code></a></td><td>Convert radians to degrees</td></tr><tr><td><a href="mysqlqb_functions.html#function_exp"><code class="literal">EXP()</code></a></td><td>Raise to the power of</td></tr><tr><td><a href="mysqlqb_functions.html#function_floor"><code class="literal">FLOOR()</code></a></td><td>Return the largest integer value not greater than the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_ln"><code class="literal">LN()</code></a></td><td>Return the natural logarithm of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_log10"><code class="literal">LOG10()</code></a></td><td>Return the base-10 logarithm of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_log2"><code class="literal">LOG2()</code></a></td><td>Return the base-2 logarithm of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_log"><code class="literal">LOG()</code></a></td><td> Return the natural logarithm of the first argument </td></tr><tr><td><a href="mysqlqb_functions.html#function_mod"><code class="literal">MOD()</code></a></td><td>Return the remainder</td></tr><tr><td><a href="mysqlqb_functions.html#function_pi"><code class="literal">PI()</code></a></td><td>Return the value of pi</td></tr><tr><td><a href="mysqlqb_functions.html#function_pow"><code class="literal">POW()</code>, <code class="literal">POWER()</code></a></td><td>Return the argument raised to the specified power</td></tr><tr><td><a href="mysqlqb_functions.html#function_radians"><code class="literal">RADIANS()</code></a></td><td>Return argument converted to radians</td></tr><tr><td><a href="mysqlqb_functions.html#function_rand"><code class="literal">RAND()</code></a></td><td>Return a random floating-point value</td></tr><tr><td><a href="mysqlqb_functions.html#function_round"><code class="literal">ROUND()</code></a></td><td>Round the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_sign"><code class="literal">SIGN()</code></a></td><td>Return the sign of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_sin"><code class="literal">SIN()</code></a></td><td>Return the sine of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_sqrt"><code class="literal">SQRT()</code></a></td><td>Return the square root of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_tan"><code class="literal">TAN()</code></a></td><td>Return the tangent of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_truncate"><code class="literal">TRUNCATE()</code></a></td><td>Truncate to specified number of decimal places</td></tr></tbody></table></div><p>
All mathematical functions return <code class="literal">NULL</code> in the
event of an error.
</p><a id="id2830127" class="indexterm"></a><a id="id2830134" class="indexterm"></a><div class="itemizedlist"><ul type="disc"><li><p><a id="function_abs"></a>
<a id="id2830164" class="indexterm"></a>
<code class="literal">ABS(<em class="replaceable"><code>X</code></em>)</code>
</p><p>
Returns the absolute value of <em class="replaceable"><code>X</code></em>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT ABS(2);</code></strong>
-> 2
mysql> <strong class="userinput"><code>SELECT ABS(-32);</code></strong>
-> 32
</pre><p>
This function is safe to use with <code class="literal">BIGINT</code>
values.
</p></li><li><p><a id="function_acos"></a>
<a id="id2830243" class="indexterm"></a>
<code class="literal">ACOS(<em class="replaceable"><code>X</code></em>)</code>
</p><p>
Returns the arc cosine of <em class="replaceable"><code>X</code></em>, that
is, the value whose cosine is <em class="replaceable"><code>X</code></em>.
Returns <code class="literal">NULL</code> if
<em class="replaceable"><code>X</code></em> is not in the range
<code class="literal">-1</code> to <code class="literal">1</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT ACOS(1);</code></strong>
-> 0
mysql> <strong class="userinput"><code>SELECT ACOS(1.0001);</code></strong>
-> NULL
mysql> <strong class="userinput"><code>SELECT ACOS(0);</code></strong>
-> 1.5707963267949
</pre></li><li><p><a id="function_asin"></a>
<a id="id2830339" class="indexterm"></a>
<code class="literal">ASIN(<em class="replaceable"><code>X</code></em>)</code>
</p><p>
Returns the arc sine of <em class="replaceable"><code>X</code></em>, that
is, the value whose sine is <em class="replaceable"><code>X</code></em>.
Returns <code class="literal">NULL</code> if
<em class="replaceable"><code>X</code></em> is not in the range
<code class="literal">-1</code> to <code class="literal">1</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT ASIN(0.2);</code></strong>
-> 0.20135792079033
mysql> <strong class="userinput"><code>SELECT ASIN('foo');</code></strong>
+-------------+
| ASIN('foo') |
+-------------+
| 0 |
+-------------+
1 row in set, 1 warning (0.00 sec)
mysql> <strong class="userinput"><code>SHOW WARNINGS;</code></strong>
+---------+------+-----------------------------------------+
| Level | Code | Message |
+---------+------+-----------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: 'foo' |
+---------+------+-----------------------------------------+
</pre></li><li><p><a id="function_atan"></a>
<a id="id2830443" class="indexterm"></a>
<code class="literal">ATAN(<em class="replaceable"><code>X</code></em>)</code>
</p><p>
Returns the arc tangent of <em class="replaceable"><code>X</code></em>,
that is, the value whose tangent is
<em class="replaceable"><code>X</code></em>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT ATAN(2);</code></strong>
-> 1.1071487177941
mysql> <strong class="userinput"><code>SELECT ATAN(-2);</code></strong>
-> -1.1071487177941
</pre></li><li><p><a id="function_atan2"></a>
<a id="id2830521" class="indexterm"></a>
<code class="literal">ATAN(<em class="replaceable"><code>Y</code></em>,<em class="replaceable"><code>X</code></em>)</code>,
<code class="literal">ATAN2(<em class="replaceable"><code>Y</code></em>,<em class="replaceable"><code>X</code></em>)</code>
</p><p>
Returns the arc tangent of the two variables
<em class="replaceable"><code>X</code></em> and
<em class="replaceable"><code>Y</code></em>. It is similar to calculating
the arc tangent of <code class="literal"><em class="replaceable"><code>Y</code></em> /
<em class="replaceable"><code>X</code></em></code>, except that the
signs of both arguments are used to determine the quadrant
of the result.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT ATAN(-2,2);</code></strong>
-> -0.78539816339745
mysql> <strong class="userinput"><code>SELECT ATAN2(PI(),0);</code></strong>
-> 1.5707963267949
</pre></li><li><p><a id="function_ceiling"></a>
<a id="id2830629" class="indexterm"></a>
<code class="literal">CEILING(<em class="replaceable"><code>X</code></em>)</code>,
<code class="literal">CEIL(<em class="replaceable"><code>X</code></em>)</code>
</p><p>
Returns the smallest integer value not less than
<em class="replaceable"><code>X</code></em>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT CEILING(1.23);</code></strong>
-> 2
mysql> <strong class="userinput"><code>SELECT CEIL(-1.23);</code></strong>
-> -1
</pre><p>
These two functions are synonymous. Note that the return
value is converted to a <code class="literal">BIGINT</code>.
</p></li><li><p><a id="function_cos"></a>
<a id="id2830717" class="indexterm"></a>
<code class="literal">COS(<em class="replaceable"><code>X</code></em>)</code>
</p><p>
Returns the cosine of <em class="replaceable"><code>X</code></em>, where
<em class="replaceable"><code>X</code></em> is given in radians.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT COS(PI());</code></strong>
-> -1
</pre></li><li><p><a id="function_cot"></a>
<a id="id2830787" class="indexterm"></a>
<code class="literal">COT(<em class="replaceable"><code>X</code></em>)</code>
</p><p>
Returns the cotangent of <em class="replaceable"><code>X</code></em>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT COT(12);</code></strong>
-> -1.5726734063977
mysql> <strong class="userinput"><code>SELECT COT(0);</code></strong>
-> NULL
</pre></li><li><p><a id="function_crc32"></a>
<a id="id2830860" class="indexterm"></a>
<code class="literal">CRC32(<em class="replaceable"><code>expr</code></em>)</code>
</p><p>
Computes a cyclic redundancy check value and returns a
32-bit unsigned value. The result is <code class="literal">NULL</code>
if the argument is <code class="literal">NULL</code>. The argument is
expected to be a string and (if possible) is treated as one
if it is not.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT CRC32('MySQL');</code></strong>
-> 3259397556
mysql> <strong class="userinput"><code>SELECT CRC32('mysql');</code></strong>
-> 2501908538
</pre></li><li><p><a id="function_degrees"></a>
<a id="id2830940" class="indexterm"></a>
<code class="literal">DEGREES(<em class="replaceable"><code>X</code></em>)</code>
</p><p>
Returns the argument <em class="replaceable"><code>X</code></em>, converted
from radians to degrees.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT DEGREES(PI());</code></strong>
-> 180
mysql> <strong class="userinput"><code>SELECT DEGREES(PI() / 2);</code></strong>
-> 90
</pre></li><li><p><a id="function_exp"></a>
<a id="id2831012" class="indexterm"></a>
<code class="literal">EXP(<em class="replaceable"><code>X</code></em>)</code>
</p><p>
Returns the value of <span class="emphasis"><em>e</em></span> (the base of
natural logarithms) raised to the power of
<em class="replaceable"><code>X</code></em>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT EXP(2);</code></strong>
-> 7.3890560989307
mysql> <strong class="userinput"><code>SELECT EXP(-2);</code></strong>
-> 0.13533528323661
mysql> <strong class="userinput"><code>SELECT EXP(0);</code></strong>
-> 1
</pre></li><li><p><a id="function_floor"></a>
<a id="id2831095" class="indexterm"></a>
<code class="literal">FLOOR(<em class="replaceable"><code>X</code></em>)</code>
</p><p>
Returns the largest integer value not greater than
<em class="replaceable"><code>X</code></em>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT FLOOR(1.23);</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT FLOOR(-1.23);</code></strong>
-> -2
</pre><p>
Note that the return value is converted to a
<code class="literal">BIGINT</code>.
</p></li><li><p>
<code class="literal">FORMAT(<em class="replaceable"><code>X</code></em>,<em class="replaceable"><code>D</code></em>)</code>
</p><p>
Formats the number <em class="replaceable"><code>X</code></em> to a format
like <code class="literal">'#,###,###.##'</code>, rounded to
<em class="replaceable"><code>D</code></em> decimal places, and returns the
result as a string. For details, see
<a href="mysqlqb_functions.html#string-functions" title="String Functions">String Functions</a>.
</p></li><li><p><a id="function_ln"></a>
<a id="id2831221" class="indexterm"></a>
<code class="literal">LN(<em class="replaceable"><code>X</code></em>)</code>
</p><p>
Returns the natural logarithm of
<em class="replaceable"><code>X</code></em>; that is, the
base-<span class="emphasis"><em>e</em></span> logarithm of
<em class="replaceable"><code>X</code></em>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT LN(2);</code></strong>
-> 0.69314718055995
mysql> <strong class="userinput"><code>SELECT LN(-2);</code></strong>
-> NULL
</pre><p>
This function is synonymous with
<code class="literal">LOG(<em class="replaceable"><code>X</code></em>)</code>.
</p></li><li><p><a id="function_log"></a>
<a id="id2831312" class="indexterm"></a>
<code class="literal">LOG(<em class="replaceable"><code>X</code></em>)</code>,
<code class="literal">LOG(<em class="replaceable"><code>B</code></em>,<em class="replaceable"><code>X</code></em>)</code>
</p><p>
If called with one parameter, this function returns the
natural logarithm of <em class="replaceable"><code>X</code></em>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT LOG(2);</code></strong>
-> 0.69314718055995
mysql> <strong class="userinput"><code>SELECT LOG(-2);</code></strong>
-> NULL
</pre><p>
If called with two parameters, this function returns the
logarithm of <em class="replaceable"><code>X</code></em> for an arbitrary
base <em class="replaceable"><code>B</code></em>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT LOG(2,65536);</code></strong>
-> 16
mysql> <strong class="userinput"><code>SELECT LOG(10,100);</code></strong>
-> 2
</pre><p>
<code class="literal">LOG(<em class="replaceable"><code>B</code></em>,<em class="replaceable"><code>X</code></em>)</code>
is equivalent to <code class="literal">LOG(<em class="replaceable"><code>X</code></em>)
/ LOG(<em class="replaceable"><code>B</code></em>)</code>.
</p></li><li><p><a id="function_log2"></a>
<a id="id2831445" class="indexterm"></a>
<code class="literal">LOG2(<em class="replaceable"><code>X</code></em>)</code>
</p><p>
Returns the base-2 logarithm of
<code class="literal"><em class="replaceable"><code>X</code></em></code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT LOG2(65536);</code></strong>
-> 16
mysql> <strong class="userinput"><code>SELECT LOG2(-100);</code></strong>
-> NULL
</pre><p>
<code class="literal">LOG2()</code> is useful for finding out how many
bits a number requires for storage. This function is
equivalent to the expression
<code class="literal">LOG(<em class="replaceable"><code>X</code></em>) /
LOG(2)</code>.
</p></li><li><p><a id="function_log10"></a>
<a id="id2831534" class="indexterm"></a>
<code class="literal">LOG10(<em class="replaceable"><code>X</code></em>)</code>
</p><p>
Returns the base-10 logarithm of
<em class="replaceable"><code>X</code></em>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT LOG10(2);</code></strong>
-> 0.30102999566398
mysql> <strong class="userinput"><code>SELECT LOG10(100);</code></strong>
-> 2
mysql> <strong class="userinput"><code>SELECT LOG10(-100);</code></strong>
-> NULL
</pre><p>
<code class="literal">LOG10(<em class="replaceable"><code>X</code></em>)</code> is
equivalent to
<code class="literal">LOG(10,<em class="replaceable"><code>X</code></em>)</code>.
</p></li><li><p><a id="function_mod"></a>
<a id="id2831634" class="indexterm"></a>
<a id="id2831641" class="indexterm"></a>
<a id="id2831648" class="indexterm"></a>
<a id="id2831655" class="indexterm"></a>
<a id="id2831661" class="indexterm"></a>
<code class="literal">MOD(<em class="replaceable"><code>N</code></em>,<em class="replaceable"><code>M</code></em>)</code>,
<code class="literal"><em class="replaceable"><code>N</code></em> %
<em class="replaceable"><code>M</code></em></code>,
<code class="literal"><em class="replaceable"><code>N</code></em> MOD
<em class="replaceable"><code>M</code></em></code>
</p><p>
Modulo operation. Returns the remainder of
<em class="replaceable"><code>N</code></em> divided by
<em class="replaceable"><code>M</code></em>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT MOD(234, 10);</code></strong>
-> 4
mysql> <strong class="userinput"><code>SELECT 253 % 7;</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT MOD(29,9);</code></strong>
-> 2
mysql> <strong class="userinput"><code>SELECT 29 MOD 9;</code></strong>
-> 2
</pre><p>
This function is safe to use with <code class="literal">BIGINT</code>
values.
</p><p>
<code class="literal">MOD()</code> also works on values that have a
fractional part and returns the exact remainder after
division:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT MOD(34.5,3);</code></strong>
-> 1.5
</pre><p>
<code class="literal">MOD(<em class="replaceable"><code>N</code></em>,0)</code>
returns <code class="literal">NULL</code>.
</p></li><li><p><a id="function_pi"></a>
<a id="id2831804" class="indexterm"></a>
<code class="literal">PI()</code>
</p><p>
Returns the value of π (pi). The default number of
decimal places displayed is seven, but MySQL uses the full
double-precision value internally.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT PI();</code></strong>
-> 3.141593
mysql> <strong class="userinput"><code>SELECT PI()+0.000000000000000000;</code></strong>
-> 3.141592653589793116
</pre></li><li><p><a id="function_pow"></a>
<a id="id2831882" class="indexterm"></a>
<a id="id2831889" class="indexterm"></a>
<code class="literal">POW(<em class="replaceable"><code>X</code></em>,<em class="replaceable"><code>Y</code></em>)</code>,
<code class="literal">POWER(<em class="replaceable"><code>X</code></em>,<em class="replaceable"><code>Y</code></em>)</code>
</p><p>
Returns the value of <em class="replaceable"><code>X</code></em> raised to
the power of <em class="replaceable"><code>Y</code></em>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT POW(2,2);</code></strong>
-> 4
mysql> <strong class="userinput"><code>SELECT POW(2,-2);</code></strong>
-> 0.25
</pre></li><li><p><a id="function_radians"></a>
<a id="id2831978" class="indexterm"></a>
<code class="literal">RADIANS(<em class="replaceable"><code>X</code></em>)</code>
</p><p>
Returns the argument <em class="replaceable"><code>X</code></em>, converted
from degrees to radians. (Note that π radians equals 180
degrees.)
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT RADIANS(90);</code></strong>
-> 1.5707963267949
</pre></li><li><p><a id="function_rand"></a>
<a id="id2832049" class="indexterm"></a>
<code class="literal">RAND()</code>,
<code class="literal">RAND(<em class="replaceable"><code>N</code></em>)</code>
</p><p>
Returns a random floating-point value
<em class="replaceable"><code>v</code></em> in the range
<code class="literal">0</code> <= <em class="replaceable"><code>v</code></em> <
<code class="literal">1.0</code>. If a constant integer argument
<em class="replaceable"><code>N</code></em> is specified, it is used as the
seed value, which produces a repeatable sequence of column
values.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT RAND();</code></strong>
-> 0.9233482386203
mysql> <strong class="userinput"><code>SELECT RAND(20);</code></strong>
-> 0.15888261251047
mysql> <strong class="userinput"><code>SELECT RAND(20);</code></strong>
-> 0.15888261251047
mysql> <strong class="userinput"><code>SELECT RAND();</code></strong>
-> 0.63553050033332
mysql> <strong class="userinput"><code>SELECT RAND();</code></strong>
-> 0.70100469486881
mysql> <strong class="userinput"><code>SELECT RAND(20);</code></strong>
-> 0.15888261251047
</pre><p>
The effect of using a non-constant argument is undefined. As
of MySQL 5.0.13, non-constant arguments are disallowed.
</p><p>
To obtain a random integer <em class="replaceable"><code>R</code></em> in
the range <em class="replaceable"><code>i</code></em> <=
<em class="replaceable"><code>R</code></em> <
<em class="replaceable"><code>j</code></em>, use the expression
<code class="literal">FLOOR(<em class="replaceable"><code>i</code></em> + RAND() *
(<em class="replaceable"><code>j</code></em> –
<em class="replaceable"><code>i</code></em>)</code>. For example, to
obtain a random integer in the range the range
<code class="literal">7</code> <= <em class="replaceable"><code>R</code></em> <
<code class="literal">12</code>, you could use the following
statement:
</p><pre class="programlisting">SELECT FLOOR(7 + (RAND() * 5));
</pre><p>
You cannot use a column with <code class="literal">RAND()</code>
values in an <code class="literal">ORDER BY</code> clause, because
<code class="literal">ORDER BY</code> would evaluate the column
multiple times. However, you can retrieve rows in random
order like this:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT * FROM <em class="replaceable"><code>tbl_name</code></em> ORDER BY RAND();</code></strong>
</pre><p>
<code class="literal">ORDER BY RAND()</code> combined with
<code class="literal">LIMIT</code> is useful for selecting a random
sample from a set of rows:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT * FROM table1, table2 WHERE a=b AND c<d</code></strong> -> <strong class="userinput"><code>ORDER BY RAND() LIMIT 1000;</code></strong>
</pre><p>
Note that <code class="literal">RAND()</code> in a
<code class="literal">WHERE</code> clause is re-evaluated every time
the <code class="literal">WHERE</code> is executed.
</p><p>
<code class="literal">RAND()</code> is not meant to be a perfect
random generator, but instead is a fast way to generate
<span class="foreignphrase"><em class="foreignphrase">ad hoc</em></span> random numbers which
is portable between platforms for the same MySQL version.
</p></li><li><p><a id="function_round"></a>
<a id="id2832307" class="indexterm"></a>
<code class="literal">ROUND(<em class="replaceable"><code>X</code></em>)</code>,
<code class="literal">ROUND(<em class="replaceable"><code>X</code></em>,<em class="replaceable"><code>D</code></em>)</code>
</p><p>
Rounds the argument <em class="replaceable"><code>X</code></em> to
<em class="replaceable"><code>D</code></em> decimal places. The rounding
algorithm depends on the data type of
<em class="replaceable"><code>X</code></em>. <em class="replaceable"><code>D</code></em>
defaults to 0 if not specified. <em class="replaceable"><code>D</code></em>
can be negative to cause <em class="replaceable"><code>D</code></em> digits
left of the decimal point of the value
<em class="replaceable"><code>X</code></em> to become zero.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT ROUND(-1.23);</code></strong>
-> -1
mysql> <strong class="userinput"><code>SELECT ROUND(-1.58);</code></strong>
-> -2
mysql> <strong class="userinput"><code>SELECT ROUND(1.58);</code></strong>
-> 2
mysql> <strong class="userinput"><code>SELECT ROUND(1.298, 1);</code></strong>
-> 1.3
mysql> <strong class="userinput"><code>SELECT ROUND(1.298, 0);</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT ROUND(23.298, -1);</code></strong>
-> 20
</pre><p>
The return type is the same type as that of the first
argument (assuming that it is integer, double, or decimal).
This means that for an integer argument, the result is an
integer (no decimal places).
</p><p>
Before MySQL 5.0.3, the behavior of
<code class="literal">ROUND()</code> when the argument is halfway
between two integers depends on the C library
implementation. Different implementations round to the
nearest even number, always up, always down, or always
toward zero. If you need one kind of rounding, you should
use a well-defined function such as
<code class="literal">TRUNCATE()</code> or <code class="literal">FLOOR()</code>
instead.
</p><p>
As of MySQL 5.0.3, <code class="literal">ROUND()</code> uses the
precision math library for exact-value arguments when the
first argument is a decimal value:
</p><div class="itemizedlist"><ul type="circle"><li><p>
For exact-value numbers, <code class="literal">ROUND()</code> uses
the “<span class="quote">round half up</span>” or “<span class="quote">round toward
nearest</span>” rule: A value with a fractional part of
.5 or greater is rounded up to the next integer if
positive or down to the next integer if negative. (In
other words, it is rounded away from zero.) A value with
a fractional part less than .5 is rounded down to the
next integer if positive or up to the next integer if
negative.
</p></li><li><p>
For approximate-value numbers, the result depends on the
C library. On many systems, this means that
<code class="literal">ROUND()</code> uses the "round to nearest
even" rule: A value with any fractional part is rounded
to the nearest even integer.
</p></li></ul></div><p>
The following example shows how rounding differs for exact
and approximate values:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT ROUND(2.5), ROUND(25E-1);</code></strong>
+------------+--------------+
| ROUND(2.5) | ROUND(25E-1) |
+------------+--------------+
| 3 | 2 |
+------------+--------------+
</pre><p>
For more information, see <a href="http://dev.mysql.com/doc/refman/5.0/en/precision-math.html" target="_top">Precision Math</a>.
</p></li><li><p><a id="function_sign"></a>
<a id="id2832541" class="indexterm"></a>
<code class="literal">SIGN(<em class="replaceable"><code>X</code></em>)</code>
</p><p>
Returns the sign of the argument as <code class="literal">-1</code>,
<code class="literal">0</code>, or <code class="literal">1</code>, depending on
whether <em class="replaceable"><code>X</code></em> is negative, zero, or
positive.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT SIGN(-32);</code></strong>
-> -1
mysql> <strong class="userinput"><code>SELECT SIGN(0);</code></strong>
-> 0
mysql> <strong class="userinput"><code>SELECT SIGN(234);</code></strong>
-> 1
</pre></li><li><p><a id="function_sin"></a>
<a id="id2832628" class="indexterm"></a>
<code class="literal">SIN(<em class="replaceable"><code>X</code></em>)</code>
</p><p>
Returns the sine of <em class="replaceable"><code>X</code></em>, where
<em class="replaceable"><code>X</code></em> is given in radians.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT SIN(PI());</code></strong>
-> 1.2246063538224e-16
mysql> <strong class="userinput"><code>SELECT ROUND(SIN(PI()));</code></strong>
-> 0
</pre></li><li><p><a id="function_sqrt"></a>
<a id="id2832705" class="indexterm"></a>
<code class="literal">SQRT(<em class="replaceable"><code>X</code></em>)</code>
</p><p>
Returns the square root of a non-negative number
<em class="replaceable"><code>X</code></em>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT SQRT(4);</code></strong>
-> 2
mysql> <strong class="userinput"><code>SELECT SQRT(20);</code></strong>
-> 4.4721359549996
mysql> <strong class="userinput"><code>SELECT SQRT(-16);</code></strong>
-> NULL
</pre></li><li><p><a id="function_tan"></a>
<a id="id2832782" class="indexterm"></a>
<code class="literal">TAN(<em class="replaceable"><code>X</code></em>)</code>
</p><p>
Returns the tangent of <em class="replaceable"><code>X</code></em>, where
<em class="replaceable"><code>X</code></em> is given in radians.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT TAN(PI());</code></strong>
-> -1.2246063538224e-16
mysql> <strong class="userinput"><code>SELECT TAN(PI()+1);</code></strong>
-> 1.5574077246549
</pre></li><li><p><a id="function_truncate"></a>
<a id="id2832860" class="indexterm"></a>
<code class="literal">TRUNCATE(<em class="replaceable"><code>X</code></em>,<em class="replaceable"><code>D</code></em>)</code>
</p><p>
Returns the number <em class="replaceable"><code>X</code></em>, truncated
to <em class="replaceable"><code>D</code></em> decimal places. If
<em class="replaceable"><code>D</code></em> is <code class="literal">0</code>, the
result has no decimal point or fractional part.
<em class="replaceable"><code>D</code></em> can be negative to cause
<em class="replaceable"><code>D</code></em> digits left of the decimal
point of the value <em class="replaceable"><code>X</code></em> to become
zero.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT TRUNCATE(1.223,1);</code></strong>
-> 1.2
mysql> <strong class="userinput"><code>SELECT TRUNCATE(1.999,1);</code></strong>
-> 1.9
mysql> <strong class="userinput"><code>SELECT TRUNCATE(1.999,0);</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT TRUNCATE(-1.999,1);</code></strong>
-> -1.9
mysql> <strong class="userinput"><code>SELECT TRUNCATE(122,-2);</code></strong>
-> 100
mysql> <strong class="userinput"><code>SELECT TRUNCATE(10.28*100,0);</code></strong>
-> 1028
</pre><p>
All numbers are rounded toward zero.
</p></li></ul></div></div></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="date-and-time-functions"></a>Date and Time Functions</h2></div></div></div><a id="id2832982" class="indexterm"></a><a id="id2832989" class="indexterm"></a><p>
This section describes the functions that can be used to
manipulate temporal values. See
<a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-types.html" target="_top">Date and Time Types</a>, for a description of the
range of values each date and time type has and the valid formats
in which values may be specified.
</p><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><thead><tr><th><span class="bold"><strong>Name</strong></span></th><th><span class="bold"><strong>Description</strong></span></th></tr></thead><tbody><tr><td><a href="mysqlqb_functions.html#function_adddate"><code class="literal">ADDDATE()</code></a></td><td>Add dates</td></tr><tr><td><a href="mysqlqb_functions.html#function_addtime"><code class="literal">ADDTIME()</code></a></td><td>Add time</td></tr><tr><td><a href="mysqlqb_functions.html#function_convert-tz"><code class="literal">CONVERT_TZ()</code></a></td><td>Convert from one timezone to another</td></tr><tr><td><a href="mysqlqb_functions.html#function_curdate"><code class="literal">CURDATE()</code></a></td><td>Return the current date</td></tr><tr><td><a href="mysqlqb_functions.html#function_current-date"><code class="literal">CURRENT_DATE()</code>, <code class="literal">CURRENT_DATE</code></a></td><td>Synonyms for CURDATE()</td></tr><tr><td><a href="mysqlqb_functions.html#function_current-time"><code class="literal">CURRENT_TIME()</code>, <code class="literal">CURRENT_TIME</code></a></td><td>Synonyms for CURTIME()</td></tr><tr><td><a href="mysqlqb_functions.html#function_current-timestamp"><code class="literal">CURRENT_TIMESTAMP()</code>, <code class="literal">CURRENT_TIMESTAMP</code></a></td><td>Synonyms for NOW()</td></tr><tr><td><a href="mysqlqb_functions.html#function_curtime"><code class="literal">CURTIME()</code></a></td><td>Return the current time</td></tr><tr><td><a href="mysqlqb_functions.html#function_date-add"><code class="literal">DATE_ADD()</code></a></td><td>Add two dates</td></tr><tr><td><a href="mysqlqb_functions.html#function_date-format"><code class="literal">DATE_FORMAT()</code></a></td><td>Format date as specified</td></tr><tr><td><a href="mysqlqb_functions.html#function_date-sub"><code class="literal">DATE_SUB()</code></a></td><td>Subtract two dates</td></tr><tr><td><a href="mysqlqb_functions.html#function_date"><code class="literal">DATE()</code></a></td><td>Extract the date part of a date or datetime expression</td></tr><tr><td><a href="mysqlqb_functions.html#function_datediff"><code class="literal">DATEDIFF()</code></a></td><td>Subtract two dates</td></tr><tr><td><a href="mysqlqb_functions.html#function_day"><code class="literal">DAY()</code></a></td><td>Synonym for DAYOFMONTH()</td></tr><tr><td><a href="mysqlqb_functions.html#function_dayname"><code class="literal">DAYNAME()</code></a></td><td>Return the name of the weekday</td></tr><tr><td><a href="mysqlqb_functions.html#function_dayofmonth"><code class="literal">DAYOFMONTH()</code></a></td><td>Return the day of the month (1-31)</td></tr><tr><td><a href="mysqlqb_functions.html#function_dayofweek"><code class="literal">DAYOFWEEK()</code></a></td><td>Return the weekday index of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_dayofyear"><code class="literal">DAYOFYEAR()</code></a></td><td>Return the day of the year (1-366)</td></tr><tr><td><a href="mysqlqb_functions.html#function_extract"><code class="literal">EXTRACT</code></a></td><td>Extract part of a date</td></tr><tr><td><a href="mysqlqb_functions.html#function_from-days"><code class="literal">FROM_DAYS()</code></a></td><td>Convert a day number to a date</td></tr><tr><td><a href="mysqlqb_functions.html#function_from-unixtime"><code class="literal">FROM_UNIXTIME()</code></a></td><td>Format date as a UNIX timestamp</td></tr><tr><td><a href="mysqlqb_functions.html#function_get-format"><code class="literal">GET_FORMAT()</code></a></td><td>Return a date format string</td></tr><tr><td><a href="mysqlqb_functions.html#function_hour"><code class="literal">HOUR()</code></a></td><td>Extract the hour</td></tr><tr><td><a href="mysqlqb_functions.html#function_last-day"><code class="literal">LAST_DAY</code></a></td><td>Return the last day of the month for the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_localtime"><code class="literal">LOCALTIME()</code>, <code class="literal">LOCALTIME</code></a></td><td>Synonym for NOW()</td></tr><tr><td><a href="mysqlqb_functions.html#function_localtimestamp"><code class="literal">LOCALTIMESTAMP</code>, <code class="literal">LOCALTIMESTAMP()</code></a></td><td>Synonym for NOW()</td></tr><tr><td><a href="mysqlqb_functions.html#function_makedate"><code class="literal">MAKEDATE()</code></a></td><td>Create a date from the year and day of year</td></tr><tr><td><a href="mysqlqb_functions.html#function_maketime"><code class="literal">MAKETIME</code></a></td><td>MAKETIME()</td></tr><tr><td><a href="mysqlqb_functions.html#function_microsecond"><code class="literal">MICROSECOND()</code></a></td><td>Return the microseconds from argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_minute"><code class="literal">MINUTE()</code></a></td><td>Return the minute from the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_month"><code class="literal">MONTH()</code></a></td><td>Return the month from the date passed</td></tr><tr><td><a href="mysqlqb_functions.html#function_monthname"><code class="literal">MONTHNAME()</code></a></td><td>Return the name of the month</td></tr><tr><td><a href="mysqlqb_functions.html#function_now"><code class="literal">NOW()</code></a></td><td>Return the current date and time</td></tr><tr><td><a href="mysqlqb_functions.html#function_period-add"><code class="literal">PERIOD_ADD()</code></a></td><td>Add a period to a year-month</td></tr><tr><td><a href="mysqlqb_functions.html#function_period-diff"><code class="literal">PERIOD_DIFF()</code></a></td><td>Return the number of months between periods</td></tr><tr><td><a href="mysqlqb_functions.html#function_quarter"><code class="literal">QUARTER()</code></a></td><td>Return the quarter from a date argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_sec-to-time"><code class="literal">SEC_TO_TIME()</code></a></td><td>Converts seconds to 'HH:MM:SS' format</td></tr><tr><td><a href="mysqlqb_functions.html#function_second"><code class="literal">SECOND()</code></a></td><td>Return the second (0-59)</td></tr><tr><td><a href="mysqlqb_functions.html#function_str-to-date"><code class="literal">STR_TO_DATE()</code></a></td><td>Convert a string to a date</td></tr><tr><td><a href="mysqlqb_functions.html#function_subdate"><code class="literal">SUBDATE()</code></a></td><td>When invoked with three arguments a synonym for DATE_SUB()</td></tr><tr><td><a href="mysqlqb_functions.html#function_subtime"><code class="literal">SUBTIME()</code></a></td><td>Subtract times</td></tr><tr><td><a href="mysqlqb_functions.html#function_sysdate"><code class="literal">SYSDATE()</code></a></td><td>Return the time at which the function executes</td></tr><tr><td><a href="mysqlqb_functions.html#function_time-format"><code class="literal">TIME_FORMAT()</code></a></td><td>Format as time</td></tr><tr><td><a href="mysqlqb_functions.html#function_time-to-sec"><code class="literal">TIME_TO_SEC()</code></a></td><td>Return the argument converted to seconds</td></tr><tr><td><a href="mysqlqb_functions.html#function_time"><code class="literal">TIME()</code></a></td><td>Extract the time portion of the expression passed</td></tr><tr><td><a href="mysqlqb_functions.html#function_timediff"><code class="literal">TIMEDIFF()</code></a></td><td>Subtract time</td></tr><tr><td><a href="mysqlqb_functions.html#function_timestamp"><code class="literal">TIMESTAMP()</code></a></td><td>With a single argument, this function returns the date or datetime expression. With two arguments, the sum of the arguments</td></tr><tr><td><a href="mysqlqb_functions.html#function_timestampadd"><code class="literal">TIMESTAMPADD()</code></a></td><td>Add an interval to a datetime expression</td></tr><tr><td><a href="mysqlqb_functions.html#function_timestampdiff"><code class="literal">TIMESTAMPDIFF()</code></a></td><td>Subtract an interval from a datetime expression</td></tr><tr><td><a href="mysqlqb_functions.html#function_to-days"><code class="literal">TO_DAYS()</code></a></td><td>Return the date argument converted to days</td></tr><tr><td><a href="mysqlqb_functions.html#function_unix-timestamp"><code class="literal">UNIX_TIMESTAMP()</code></a></td><td>Return a UNIX timestamp</td></tr><tr><td><a href="mysqlqb_functions.html#function_utc-date"><code class="literal">UTC_DATE()</code></a></td><td>Return the current UTC date</td></tr><tr><td><a href="mysqlqb_functions.html#function_utc-time"><code class="literal">UTC_TIME()</code></a></td><td>Return the current UTC time</td></tr><tr><td><a href="mysqlqb_functions.html#function_utc-timestamp"><code class="literal">UTC_TIMESTAMP()</code></a></td><td>Return the current UTC date and time</td></tr><tr><td><a href="mysqlqb_functions.html#function_week"><code class="literal">WEEK()</code></a></td><td>Return the week number</td></tr><tr><td><a href="mysqlqb_functions.html#function_weekday"><code class="literal">WEEKDAY()</code></a></td><td>Return the weekday index</td></tr><tr><td><a href="mysqlqb_functions.html#function_weekofyear"><code class="literal">WEEKOFYEAR()</code></a></td><td>Return the calendar week of the date (1-53)</td></tr><tr><td><a href="mysqlqb_functions.html#function_year"><code class="literal">YEAR()</code></a></td><td>Return the year</td></tr><tr><td><a href="mysqlqb_functions.html#function_yearweek"><code class="literal">YEARWEEK()</code></a></td><td>Return the year and week</td></tr></tbody></table></div><p>
Here is an example that uses date functions. The following query
selects all rows with a <em class="replaceable"><code>date_col</code></em> value
from within the last 30 days:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT <em class="replaceable"><code>something</code></em> FROM <em class="replaceable"><code>tbl_name</code></em></code></strong>
-> <strong class="userinput"><code>WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= <em class="replaceable"><code>date_col</code></em>;</code></strong>
</pre><p>
Note that the query also selects rows with dates that lie in the
future.
</p><p>
Functions that expect date values usually accept datetime values
and ignore the time part. Functions that expect time values
usually accept datetime values and ignore the date part.
</p><p>
Functions that return the current date or time each are evaluated
only once per query at the start of query execution. This means
that multiple references to a function such as
<code class="literal">NOW()</code> within a single query always produce the
same result (for our purposes a single query also includes a call
to a stored routine or trigger and all sub-routines called by that
routine/trigger). This principle also applies to
<code class="literal">CURDATE()</code>, <code class="literal">CURTIME()</code>,
<code class="literal">UTC_DATE()</code>, <code class="literal">UTC_TIME()</code>,
<code class="literal">UTC_TIMESTAMP()</code>, and to any of their synonyms.
</p><p>
The <code class="literal">CURRENT_TIMESTAMP()</code>,
<code class="literal">CURRENT_TIME()</code>,
<code class="literal">CURRENT_DATE()</code>, and
<code class="literal">FROM_UNIXTIME()</code> functions return values in the
connection's current time zone, which is available as the value of
the <code class="literal">time_zone</code> system variable. In addition,
<code class="literal">UNIX_TIMESTAMP()</code> assumes that its argument is a
datetime value in the current time zone. See
<a href="http://dev.mysql.com/doc/refman/5.0/en/time-zone-support.html" target="_top">MySQL Server Time Zone Support</a>.
</p><p>
Some date functions can be used with “<span class="quote">zero</span>” dates or
incomplete dates such as <code class="literal">'2001-11-00'</code>, whereas
others cannot. Functions that extract parts of dates typically
work with incomplete dates. For example:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT DAYOFMONTH('2001-11-00'), MONTH('2005-00-00');</code></strong>
-> 0, 0
</pre><p>
Other functions expect complete dates and return
<code class="literal">NULL</code> for incomplete dates. These include
functions that perform date arithmetic or that map parts of dates
to names. For example:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT DATE_ADD('2006-05-00',INTERVAL 1 DAY);</code></strong>
-> NULL
mysql> <strong class="userinput"><code>SELECT DAYNAME('2006-05-00');</code></strong>
-> NULL
</pre><div class="itemizedlist"><ul type="disc"><li><p><a id="function_adddate"></a>
<a id="id2833893" class="indexterm"></a>
<code class="literal">ADDDATE(<em class="replaceable"><code>date</code></em>,INTERVAL
<em class="replaceable"><code>expr</code></em>
<em class="replaceable"><code>unit</code></em>)</code>,
<code class="literal">ADDDATE(<em class="replaceable"><code>expr</code></em>,<em class="replaceable"><code>days</code></em>)</code>
</p><p>
When invoked with the <code class="literal">INTERVAL</code> form of the
second argument, <code class="literal">ADDDATE()</code> is a synonym for
<code class="literal">DATE_ADD()</code>. The related function
<code class="literal">SUBDATE()</code> is a synonym for
<code class="literal">DATE_SUB()</code>. For information on the
<code class="literal">INTERVAL</code> <em class="replaceable"><code>unit</code></em>
argument, see the discussion for
<code class="literal">DATE_ADD()</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT DATE_ADD('1998-01-02', INTERVAL 31 DAY);</code></strong>
-> '1998-02-02'
mysql> <strong class="userinput"><code>SELECT ADDDATE('1998-01-02', INTERVAL 31 DAY);</code></strong>
-> '1998-02-02'
</pre><p>
When invoked with the <em class="replaceable"><code>days</code></em> form of
the second argument, MySQL treats it as an integer number of
days to be added to <em class="replaceable"><code>expr</code></em>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT ADDDATE('1998-01-02', 31);</code></strong>
-> '1998-02-02'
</pre></li><li><p><a id="function_addtime"></a>
<a id="id2834036" class="indexterm"></a>
<code class="literal">ADDTIME(<em class="replaceable"><code>expr1</code></em>,<em class="replaceable"><code>expr2</code></em>)</code>
</p><p>
<code class="literal">ADDTIME()</code> adds
<em class="replaceable"><code>expr2</code></em> to
<em class="replaceable"><code>expr1</code></em> and returns the result.
<em class="replaceable"><code>expr1</code></em> is a time or datetime
expression, and <em class="replaceable"><code>expr2</code></em> is a time
expression.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT ADDTIME('1997-12-31 23:59:59.999999',</code></strong>
-> <strong class="userinput"><code>'1 1:1:1.000002');</code></strong>
-> '1998-01-02 01:01:01.000001'
mysql> <strong class="userinput"><code>SELECT ADDTIME('01:00:00.999999', '02:00:00.999998');</code></strong>
-> '03:00:01.999997'
</pre></li><li><p><a id="function_convert-tz"></a>
<a id="id2834142" class="indexterm"></a>
<code class="literal">CONVERT_TZ(<em class="replaceable"><code>dt</code></em>,<em class="replaceable"><code>from_tz</code></em>,<em class="replaceable"><code>to_tz</code></em>)</code>
</p><p>
<code class="literal">CONVERT_TZ()</code> converts a datetime value
<em class="replaceable"><code>dt</code></em> from the time zone given by
<em class="replaceable"><code>from_tz</code></em> to the time zone given by
<em class="replaceable"><code>to_tz</code></em> and returns the resulting
value. Time zones are specified as described in
<a href="http://dev.mysql.com/doc/refman/5.0/en/time-zone-support.html" target="_top">MySQL Server Time Zone Support</a>. This function returns
<code class="literal">NULL</code> if the arguments are invalid.
</p><p>
If the value falls out of the supported range of the
<code class="literal">TIMESTAMP</code> type when converted fom
<em class="replaceable"><code>from_tz</code></em> to UTC, no conversion
occurs. The <code class="literal">TIMESTAMP</code> range is described in
<a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-type-overview.html" target="_top">Overview of Date and Time Types</a>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT CONVERT_TZ('2004-01-01 12:00:00','GMT','MET');</code></strong>
-> '2004-01-01 13:00:00'
mysql> <strong class="userinput"><code>SELECT CONVERT_TZ('2004-01-01 12:00:00','+00:00','+10:00');</code></strong>
-> '2004-01-01 22:00:00'
</pre><p>
<span class="bold"><strong>Note</strong></span>: To use named time zones
such as <code class="literal">'MET'</code> or
<code class="literal">'Europe/Moscow'</code>, the time zone tables must
be properly set up. See <a href="http://dev.mysql.com/doc/refman/5.0/en/time-zone-support.html" target="_top">MySQL Server Time Zone Support</a>,
for instructions.
</p><p>
If you intend to use <code class="literal">CONVERT_TZ()</code> while
other tables are locked with <code class="literal">LOCK TABLES</code>,
you must also lock the <code class="literal">mysql.time_zone_name</code>
table.
</p></li><li><p><a id="function_curdate"></a>
<a id="id2834318" class="indexterm"></a>
<code class="literal">CURDATE()</code>
</p><p>
Returns the current date as a value in
<code class="literal">'YYYY-MM-DD'</code> or <code class="literal">YYYYMMDD</code>
format, depending on whether the function is used in a string
or numeric context.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT CURDATE();</code></strong>
-> '1997-12-15'
mysql> <strong class="userinput"><code>SELECT CURDATE() + 0;</code></strong>
-> 19971215
</pre></li><li><p><a id="function_current-date"></a>
<a id="id2834394" class="indexterm"></a>
<code class="literal">CURRENT_DATE</code>,
<code class="literal">CURRENT_DATE()</code>
</p><p>
<code class="literal">CURRENT_DATE</code> and
<code class="literal">CURRENT_DATE()</code> are synonyms for
<code class="literal">CURDATE()</code>.
</p></li><li><p><a id="function_curtime"></a>
<a id="id2834457" class="indexterm"></a>
<code class="literal">CURTIME()</code>
</p><p>
Returns the current time as a value in
<code class="literal">'HH:MM:SS'</code> or <code class="literal">HHMMSS</code>
format, depending on whether the function is used in a string
or numeric context. The value is expressed in the current time
zone.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT CURTIME();</code></strong>
-> '23:50:26'
mysql> <strong class="userinput"><code>SELECT CURTIME() + 0;</code></strong>
-> 235026
</pre></li><li><p><a id="function_current-time"></a>
<a id="id2834533" class="indexterm"></a>
<code class="literal">CURRENT_TIME</code>,
<code class="literal">CURRENT_TIME()</code>
</p><p>
<code class="literal">CURRENT_TIME</code> and
<code class="literal">CURRENT_TIME()</code> are synonyms for
<code class="literal">CURTIME()</code>.
</p></li><li><p><a id="function_current-timestamp"></a>
<a id="id2834597" class="indexterm"></a>
<code class="literal">CURRENT_TIMESTAMP</code>,
<code class="literal">CURRENT_TIMESTAMP()</code>
</p><p>
<code class="literal">CURRENT_TIMESTAMP</code> and
<code class="literal">CURRENT_TIMESTAMP()</code> are synonyms for
<code class="literal">NOW()</code>.
</p></li><li><p><a id="function_date"></a>
<a id="id2834667" class="indexterm"></a>
<code class="literal">DATE(<em class="replaceable"><code>expr</code></em>)</code>
</p><p>
Extracts the date part of the date or datetime expression
<em class="replaceable"><code>expr</code></em>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT DATE('2003-12-31 01:02:03');</code></strong>
-> '2003-12-31'
</pre></li><li><p><a id="function_datediff"></a>
<a id="id2834736" class="indexterm"></a>
<code class="literal">DATEDIFF(<em class="replaceable"><code>expr1</code></em>,<em class="replaceable"><code>expr2</code></em>)</code>
</p><p>
<code class="literal">DATEDIFF()</code> returns
<em class="replaceable"><code>expr1</code></em> –
<em class="replaceable"><code>expr2</code></em> expressed as a value in days
from one date to the other. <em class="replaceable"><code>expr1</code></em>
and <em class="replaceable"><code>expr2</code></em> are date or date-and-time
expressions. Only the date parts of the values are used in the
calculation.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT DATEDIFF('1997-12-31 23:59:59','1997-12-30');</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT DATEDIFF('1997-11-30 23:59:59','1997-12-31');</code></strong>
-> -31
</pre></li><li><p><a id="function_date-add"></a>
<a id="id2834846" class="indexterm"></a>
<a id="id2834852" class="indexterm"></a>
<code class="literal">DATE_ADD(<em class="replaceable"><code>date</code></em>,INTERVAL
<em class="replaceable"><code>expr</code></em>
<em class="replaceable"><code>unit</code></em>)</code>,
<code class="literal">DATE_SUB(<em class="replaceable"><code>date</code></em>,INTERVAL
<em class="replaceable"><code>expr</code></em>
<em class="replaceable"><code>unit</code></em>)</code>
</p><p>
These functions perform date arithmetic.
<em class="replaceable"><code>date</code></em> is a
<code class="literal">DATETIME</code> or <code class="literal">DATE</code> value
specifying the starting date. <em class="replaceable"><code>expr</code></em>
is an expression specifying the interval value to be added or
subtracted from the starting date.
<em class="replaceable"><code>expr</code></em> is a string; it may start with
a ‘<code class="literal">-</code>’ for negative intervals.
<em class="replaceable"><code>unit</code></em> is a keyword indicating the
units in which the expression should be interpreted.
</p><p>
The <code class="literal">INTERVAL</code> keyword and the
<em class="replaceable"><code>unit</code></em> specifier are not case
sensitive.
</p><p>
The following table shows the expected form of the
<em class="replaceable"><code>expr</code></em> argument for each
<em class="replaceable"><code>unit</code></em> value.
</p><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><tbody><tr><td><em class="replaceable"><code>unit</code></em> <span class="bold"><strong>Value</strong></span></td><td><span class="bold"><strong>Expected</strong></span>
<em class="replaceable"><code>expr</code></em>
<span class="bold"><strong>Format</strong></span></td></tr><tr><td><code class="literal">MICROSECOND</code></td><td><code class="literal">MICROSECONDS</code></td></tr><tr><td><code class="literal">SECOND</code></td><td><code class="literal">SECONDS</code></td></tr><tr><td><code class="literal">MINUTE</code></td><td><code class="literal">MINUTES</code></td></tr><tr><td><code class="literal">HOUR</code></td><td><code class="literal">HOURS</code></td></tr><tr><td><code class="literal">DAY</code></td><td><code class="literal">DAYS</code></td></tr><tr><td><code class="literal">WEEK</code></td><td><code class="literal">WEEKS</code></td></tr><tr><td><code class="literal">MONTH</code></td><td><code class="literal">MONTHS</code></td></tr><tr><td><code class="literal">QUARTER</code></td><td><code class="literal">QUARTERS</code></td></tr><tr><td><code class="literal">YEAR</code></td><td><code class="literal">YEARS</code></td></tr><tr><td><code class="literal">SECOND_MICROSECOND</code></td><td><code class="literal">'SECONDS.MICROSECONDS'</code></td></tr><tr><td><code class="literal">MINUTE_MICROSECOND</code></td><td><code class="literal">'MINUTES.MICROSECONDS'</code></td></tr><tr><td><code class="literal">MINUTE_SECOND</code></td><td><code class="literal">'MINUTES:SECONDS'</code></td></tr><tr><td><code class="literal">HOUR_MICROSECOND</code></td><td><code class="literal">'HOURS.MICROSECONDS'</code></td></tr><tr><td><code class="literal">HOUR_SECOND</code></td><td><code class="literal">'HOURS:MINUTES:SECONDS'</code></td></tr><tr><td><code class="literal">HOUR_MINUTE</code></td><td><code class="literal">'HOURS:MINUTES'</code></td></tr><tr><td><code class="literal">DAY_MICROSECOND</code></td><td><code class="literal">'DAYS.MICROSECONDS'</code></td></tr><tr><td><code class="literal">DAY_SECOND</code></td><td><code class="literal">'DAYS HOURS:MINUTES:SECONDS'</code></td></tr><tr><td><code class="literal">DAY_MINUTE</code></td><td><code class="literal">'DAYS HOURS:MINUTES'</code></td></tr><tr><td><code class="literal">DAY_HOUR</code></td><td><code class="literal">'DAYS HOURS'</code></td></tr><tr><td><code class="literal">YEAR_MONTH</code></td><td><code class="literal">'YEARS-MONTHS'</code></td></tr></tbody></table></div><p>
The values <code class="literal">QUARTER</code> and
<code class="literal">WEEK</code> are available beginning with MySQL
5.0.0.
</p><p>
MySQL allows any punctuation delimiter in the
<em class="replaceable"><code>expr</code></em> format. Those shown in the
table are the suggested delimiters. If the
<em class="replaceable"><code>date</code></em> argument is a
<code class="literal">DATE</code> value and your calculations involve
only <code class="literal">YEAR</code>, <code class="literal">MONTH</code>, and
<code class="literal">DAY</code> parts (that is, no time parts), the
result is a <code class="literal">DATE</code> value. Otherwise, the
result is a <code class="literal">DATETIME</code> value.
</p><p>
Date arithmetic also can be performed using
<code class="literal">INTERVAL</code> together with the
<code class="literal">+</code> or <code class="literal">-</code> operator:
</p><pre class="programlisting"><code class="literal">date</code> + INTERVAL <em class="replaceable"><code>expr</code></em> <em class="replaceable"><code>unit</code></em>
<code class="literal">date</code> - INTERVAL <em class="replaceable"><code>expr</code></em> <em class="replaceable"><code>unit</code></em>
</pre><p>
<code class="literal">INTERVAL <em class="replaceable"><code>expr</code></em>
<em class="replaceable"><code>unit</code></em></code> is allowed on either
side of the <code class="literal">+</code> operator if the expression on
the other side is a date or datetime value. For the
<code class="literal">-</code> operator, <code class="literal">INTERVAL
<em class="replaceable"><code>expr</code></em>
<em class="replaceable"><code>unit</code></em></code> is allowed only on
the right side, because it makes no sense to subtract a date
or datetime value from an interval.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT '1997-12-31 23:59:59' + INTERVAL 1 SECOND;</code></strong>
-> '1998-01-01 00:00:00'
mysql> <strong class="userinput"><code>SELECT INTERVAL 1 DAY + '1997-12-31';</code></strong>
-> '1998-01-01'
mysql> <strong class="userinput"><code>SELECT '1998-01-01' - INTERVAL 1 SECOND;</code></strong>
-> '1997-12-31 23:59:59'
mysql> <strong class="userinput"><code>SELECT DATE_ADD('1997-12-31 23:59:59',</code></strong>
-> <strong class="userinput"><code>INTERVAL 1 SECOND);</code></strong>
-> '1998-01-01 00:00:00'
mysql> <strong class="userinput"><code>SELECT DATE_ADD('1997-12-31 23:59:59',</code></strong>
-> <strong class="userinput"><code>INTERVAL 1 DAY);</code></strong>
-> '1998-01-01 23:59:59'
mysql> <strong class="userinput"><code>SELECT DATE_ADD('1997-12-31 23:59:59',</code></strong>
-> <strong class="userinput"><code>INTERVAL '1:1' MINUTE_SECOND);</code></strong>
-> '1998-01-01 00:01:00'
mysql> <strong class="userinput"><code>SELECT DATE_SUB('1998-01-01 00:00:00',</code></strong>
-> <strong class="userinput"><code>INTERVAL '1 1:1:1' DAY_SECOND);</code></strong>
-> '1997-12-30 22:58:59'
mysql> <strong class="userinput"><code>SELECT DATE_ADD('1998-01-01 00:00:00',</code></strong>
-> <strong class="userinput"><code>INTERVAL '-1 10' DAY_HOUR);</code></strong>
-> '1997-12-30 14:00:00'
mysql> <strong class="userinput"><code>SELECT DATE_SUB('1998-01-02', INTERVAL 31 DAY);</code></strong>
-> '1997-12-02'
mysql> <strong class="userinput"><code>SELECT DATE_ADD('1992-12-31 23:59:59.000002',</code></strong>
-> <strong class="userinput"><code>INTERVAL '1.999999' SECOND_MICROSECOND);</code></strong>
-> '1993-01-01 00:00:01.000001'
</pre><p>
If you specify an interval value that is too short (does not
include all the interval parts that would be expected from the
<em class="replaceable"><code>unit</code></em> keyword), MySQL assumes that
you have left out the leftmost parts of the interval value.
For example, if you specify a <em class="replaceable"><code>unit</code></em>
of <code class="literal">DAY_SECOND</code>, the value of
<em class="replaceable"><code>expr</code></em> is expected to have days,
hours, minutes, and seconds parts. If you specify a value like
<code class="literal">'1:10'</code>, MySQL assumes that the days and
hours parts are missing and the value represents minutes and
seconds. In other words, <code class="literal">'1:10' DAY_SECOND</code>
is interpreted in such a way that it is equivalent to
<code class="literal">'1:10' MINUTE_SECOND</code>. This is analogous to
the way that MySQL interprets <code class="literal">TIME</code> values
as representing elapsed time rather than as a time of day.
</p><p>
If you add to or subtract from a date value something that
contains a time part, the result is automatically converted to
a datetime value:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT DATE_ADD('1999-01-01', INTERVAL 1 DAY);</code></strong>
-> '1999-01-02'
mysql> <strong class="userinput"><code>SELECT DATE_ADD('1999-01-01', INTERVAL 1 HOUR);</code></strong>
-> '1999-01-01 01:00:00'
</pre><p>
If you add <code class="literal">MONTH</code>,
<code class="literal">YEAR_MONTH</code>, or <code class="literal">YEAR</code> and
the resulting date has a day that is larger than the maximum
day for the new month, the day is adjusted to the maximum days
in the new month:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT DATE_ADD('1998-01-30', INTERVAL 1 MONTH);</code></strong>
-> '1998-02-28'
</pre><p>
Date arithmetic operations require complete dates and do not
work with incomplete dates such as
<code class="literal">'2006-07-00'</code> or badly malformed dates:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT DATE_ADD('2006-07-00', INTERVAL 1 DAY);</code></strong>
-> NULL
mysql> <strong class="userinput"><code>SELECT '2005-03-32' + INTERVAL 1 MONTH;</code></strong>
-> NULL
</pre></li><li><p><a id="function_date-format"></a>
<a id="id2835619" class="indexterm"></a>
<code class="literal">DATE_FORMAT(<em class="replaceable"><code>date</code></em>,<em class="replaceable"><code>format</code></em>)</code>
</p><p>
Formats the <em class="replaceable"><code>date</code></em> value according to
the <em class="replaceable"><code>format</code></em> string.
</p><p>
The following specifiers may be used in the
<em class="replaceable"><code>format</code></em> string. The
‘<code class="literal">%</code>’ character is required
before format specifier characters.
</p><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><tbody><tr><td><span class="bold"><strong>Specifier</strong></span></td><td><span class="bold"><strong>Description</strong></span></td></tr><tr><td><code class="literal">%a</code></td><td>Abbreviated weekday name
(<code class="literal">Sun</code>..<code class="literal">Sat</code>)</td></tr><tr><td><code class="literal">%b</code></td><td>Abbreviated month name (<code class="literal">Jan</code>..<code class="literal">Dec</code>)</td></tr><tr><td><code class="literal">%c</code></td><td>Month, numeric (<code class="literal">0</code>..<code class="literal">12</code>)</td></tr><tr><td><code class="literal">%D</code></td><td>Day of the month with English suffix (<code class="literal">0th</code>,
<code class="literal">1st</code>, <code class="literal">2nd</code>,
<code class="literal">3rd</code>, …)</td></tr><tr><td><code class="literal">%d</code></td><td>Day of the month, numeric (<code class="literal">00</code>..<code class="literal">31</code>)</td></tr><tr><td><code class="literal">%e</code></td><td>Day of the month, numeric (<code class="literal">0</code>..<code class="literal">31</code>)</td></tr><tr><td><code class="literal">%f</code></td><td>Microseconds (<code class="literal">000000</code>..<code class="literal">999999</code>)</td></tr><tr><td><code class="literal">%H</code></td><td>Hour (<code class="literal">00</code>..<code class="literal">23</code>)</td></tr><tr><td><code class="literal">%h</code></td><td>Hour (<code class="literal">01</code>..<code class="literal">12</code>)</td></tr><tr><td><code class="literal">%I</code></td><td>Hour (<code class="literal">01</code>..<code class="literal">12</code>)</td></tr><tr><td><code class="literal">%i</code></td><td>Minutes, numeric (<code class="literal">00</code>..<code class="literal">59</code>)</td></tr><tr><td><code class="literal">%j</code></td><td>Day of year (<code class="literal">001</code>..<code class="literal">366</code>)</td></tr><tr><td><code class="literal">%k</code></td><td>Hour (<code class="literal">0</code>..<code class="literal">23</code>)</td></tr><tr><td><code class="literal">%l</code></td><td>Hour (<code class="literal">1</code>..<code class="literal">12</code>)</td></tr><tr><td><code class="literal">%M</code></td><td>Month name (<code class="literal">January</code>..<code class="literal">December</code>)</td></tr><tr><td><code class="literal">%m</code></td><td>Month, numeric (<code class="literal">00</code>..<code class="literal">12</code>)</td></tr><tr><td><code class="literal">%p</code></td><td><code class="literal">AM</code> or <code class="literal">PM</code></td></tr><tr><td><code class="literal">%r</code></td><td>Time, 12-hour (<code class="literal">hh:mm:ss</code> followed by
<code class="literal">AM</code> or <code class="literal">PM</code>)</td></tr><tr><td><code class="literal">%S</code></td><td>Seconds (<code class="literal">00</code>..<code class="literal">59</code>)</td></tr><tr><td><code class="literal">%s</code></td><td>Seconds (<code class="literal">00</code>..<code class="literal">59</code>)</td></tr><tr><td><code class="literal">%T</code></td><td>Time, 24-hour (<code class="literal">hh:mm:ss</code>)</td></tr><tr><td><code class="literal">%U</code></td><td>Week (<code class="literal">00</code>..<code class="literal">53</code>), where Sunday is the
first day of the week</td></tr><tr><td><code class="literal">%u</code></td><td>Week (<code class="literal">00</code>..<code class="literal">53</code>), where Monday is the
first day of the week</td></tr><tr><td><code class="literal">%V</code></td><td>Week (<code class="literal">01</code>..<code class="literal">53</code>), where Sunday is the
first day of the week; used with <code class="literal">%X</code></td></tr><tr><td><code class="literal">%v</code></td><td>Week (<code class="literal">01</code>..<code class="literal">53</code>), where Monday is the
first day of the week; used with <code class="literal">%x</code></td></tr><tr><td><code class="literal">%W</code></td><td>Weekday name (<code class="literal">Sunday</code>..<code class="literal">Saturday</code>)</td></tr><tr><td><code class="literal">%w</code></td><td>Day of the week
(<code class="literal">0</code>=Sunday..<code class="literal">6</code>=Saturday)</td></tr><tr><td><code class="literal">%X</code></td><td>Year for the week where Sunday is the first day of the week, numeric,
four digits; used with <code class="literal">%V</code></td></tr><tr><td><code class="literal">%x</code></td><td>Year for the week, where Monday is the first day of the week, numeric,
four digits; used with <code class="literal">%v</code></td></tr><tr><td><code class="literal">%Y</code></td><td>Year, numeric, four digits</td></tr><tr><td><code class="literal">%y</code></td><td>Year, numeric (two digits)</td></tr><tr><td><code class="literal">%%</code></td><td>A literal ‘<code class="literal">%</code>’ character</td></tr><tr><td><code class="literal">%<em class="replaceable"><code>x</code></em></code></td><td><em class="replaceable"><code>x</code></em>, for any
‘<em class="replaceable"><code>x</code></em>’ not listed
above</td></tr></tbody></table></div><p>
Ranges for the month and day specifiers begin with zero due to
the fact that MySQL allows the storing of incomplete dates
such as <code class="literal">'2004-00-00'</code>.
</p><p>
As of MySQL 5.0.25, the language used for day and month names
and abbreviations is controlled by the value of the
<code class="literal">lc_time_names</code> system variable
(<a href="http://dev.mysql.com/doc/refman/5.0/en/locale-support.html" target="_top">MySQL Server Locale Support</a>).
</p><p>
As of MySQL 5.0.36, <code class="literal">DATE_FORMAT()</code> returns a
string with a character set and collation given by
<code class="literal">character_set_connection</code> and
<code class="literal">collation_connection</code> so that it can return
month and weekday names containing non-ASCII characters.
Before 5.0.36, the return value is a binary string.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT DATE_FORMAT('1997-10-04 22:23:00', '%W %M %Y');</code></strong>
-> 'Saturday October 1997'
mysql> <strong class="userinput"><code>SELECT DATE_FORMAT('1997-10-04 22:23:00', '%H:%i:%s');</code></strong>
-> '22:23:00'
mysql> <strong class="userinput"><code>SELECT DATE_FORMAT('1997-10-04 22:23:00',</code></strong>
'%D %y %a %d %m %b %j');
-> '4th 97 Sat 04 10 Oct 277'
mysql> <strong class="userinput"><code>SELECT DATE_FORMAT('1997-10-04 22:23:00',</code></strong>
'%H %k %I %r %T %S %w');
-> '22 22 10 10:23:00 PM 22:23:00 00 6'
mysql> <strong class="userinput"><code>SELECT DATE_FORMAT('1999-01-01', '%X %V');</code></strong>
-> '1998 52'
mysql> <strong class="userinput"><code>SELECT DATE_FORMAT('2006-06-00', '%d');</code></strong>
-> '00'
</pre></li><li><p><a id="function_date-sub"></a>
<a id="id2836404" class="indexterm"></a>
<code class="literal">DATE_SUB(<em class="replaceable"><code>date</code></em>,INTERVAL
<em class="replaceable"><code>expr</code></em>
<em class="replaceable"><code>unit</code></em>)</code>
</p><p>
See <code class="literal">DATE_ADD()</code>.
</p></li><li><p><a id="function_day"></a>
<a id="id2836466" class="indexterm"></a>
<code class="literal">DAY(<em class="replaceable"><code>date</code></em>)</code>
</p><p>
<code class="literal">DAY()</code> is a synonym for
<code class="literal">DAYOFMONTH()</code>.
</p></li><li><p><a id="function_dayname"></a>
<a id="id2836525" class="indexterm"></a>
<code class="literal">DAYNAME(<em class="replaceable"><code>date</code></em>)</code>
</p><p>
Returns the name of the weekday for
<em class="replaceable"><code>date</code></em>. As of MySQL 5.0.25, the
language used for the name is controlled by the value of the
<code class="literal">lc_time_names</code> system variable
(<a href="http://dev.mysql.com/doc/refman/5.0/en/locale-support.html" target="_top">MySQL Server Locale Support</a>).
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT DAYNAME('1998-02-05');</code></strong>
-> 'Thursday'
</pre></li><li><p><a id="function_dayofmonth"></a>
<a id="id2836606" class="indexterm"></a>
<code class="literal">DAYOFMONTH(<em class="replaceable"><code>date</code></em>)</code>
</p><p>
Returns the day of the month for
<em class="replaceable"><code>date</code></em>, in the range
<code class="literal">0</code> to <code class="literal">31</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT DAYOFMONTH('1998-02-03');</code></strong>
-> 3
</pre></li><li><p><a id="function_dayofweek"></a>
<a id="id2836681" class="indexterm"></a>
<code class="literal">DAYOFWEEK(<em class="replaceable"><code>date</code></em>)</code>
</p><p>
Returns the weekday index for <em class="replaceable"><code>date</code></em>
(<code class="literal">1</code> = Sunday, <code class="literal">2</code> = Monday,
…, <code class="literal">7</code> = Saturday). These index values
correspond to the ODBC standard.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT DAYOFWEEK('1998-02-03');</code></strong>
-> 3
</pre></li><li><p><a id="function_dayofyear"></a>
<a id="id2836762" class="indexterm"></a>
<code class="literal">DAYOFYEAR(<em class="replaceable"><code>date</code></em>)</code>
</p><p>
Returns the day of the year for
<em class="replaceable"><code>date</code></em>, in the range
<code class="literal">1</code> to <code class="literal">366</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT DAYOFYEAR('1998-02-03');</code></strong>
-> 34
</pre></li><li><p><a id="function_extract"></a>
<a id="id2836837" class="indexterm"></a>
<code class="literal">EXTRACT(<em class="replaceable"><code>unit</code></em> FROM
<em class="replaceable"><code>date</code></em>)</code>
</p><p>
The <code class="literal">EXTRACT()</code> function uses the same kinds
of unit specifiers as <code class="literal">DATE_ADD()</code> or
<code class="literal">DATE_SUB()</code>, but extracts parts from the
date rather than performing date arithmetic.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT EXTRACT(YEAR FROM '1999-07-02');</code></strong>
-> 1999
mysql> <strong class="userinput"><code>SELECT EXTRACT(YEAR_MONTH FROM '1999-07-02 01:02:03');</code></strong>
-> 199907
mysql> <strong class="userinput"><code>SELECT EXTRACT(DAY_MINUTE FROM '1999-07-02 01:02:03');</code></strong>
-> 20102
mysql> <strong class="userinput"><code>SELECT EXTRACT(MICROSECOND</code></strong>
-> <strong class="userinput"><code>FROM '2003-01-02 10:30:00.000123');</code></strong>
-> 123
</pre></li><li><p><a id="function_from-days"></a>
<a id="id2836939" class="indexterm"></a>
<code class="literal">FROM_DAYS(<em class="replaceable"><code>N</code></em>)</code>
</p><p>
Given a day number <em class="replaceable"><code>N</code></em>, returns a
<code class="literal">DATE</code> value.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT FROM_DAYS(729669);</code></strong>
-> '1997-10-07'
</pre><p>
Use <code class="literal">FROM_DAYS()</code> with caution on old dates.
It is not intended for use with values that precede the advent
of the Gregorian calendar (1582). See
<a href="mysqlqb_functions.html#mysql-calendar" title="What Calendar Is Used By MySQL?">What Calendar Is Used By MySQL?</a>.
</p></li><li><p><a id="function_from-unixtime"></a>
<a id="id2837026" class="indexterm"></a>
<code class="literal">FROM_UNIXTIME(<em class="replaceable"><code>unix_timestamp</code></em>)</code>,
<code class="literal">FROM_UNIXTIME(<em class="replaceable"><code>unix_timestamp</code></em>,<em class="replaceable"><code>format</code></em>)</code>
</p><p>
Returns a representation of the
<em class="replaceable"><code>unix_timestamp</code></em> argument as a value
in <code class="literal">'YYYY-MM-DD HH:MM:SS'</code> or
<code class="literal">YYYYMMDDHHMMSS</code> format, depending on whether
the function is used in a string or numeric context. The value
is expressed in the current time zone.
<em class="replaceable"><code>unix_timestamp</code></em> is an internal
timestamp value such as is produced by the
<code class="literal">UNIX_TIMESTAMP()</code> function.
</p><p>
If <em class="replaceable"><code>format</code></em> is given, the result is
formatted according to the <em class="replaceable"><code>format</code></em>
string, which is used the same way as listed in the entry for
the <code class="literal">DATE_FORMAT()</code> function.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT FROM_UNIXTIME(875996580);</code></strong>
-> '1997-10-04 22:23:00'
mysql> <strong class="userinput"><code>SELECT FROM_UNIXTIME(875996580) + 0;</code></strong>
-> 19971004222300
mysql> <strong class="userinput"><code>SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(),</code></strong>
-> <strong class="userinput"><code>'%Y %D %M %h:%i:%s %x');</code></strong>
-> '2003 6th August 06:22:58 2003'
</pre><p>
Note: If you use <code class="literal">UNIX_TIMESTAMP()</code> and
<code class="literal">FROM_UNIXTIME()</code> to convert between
<code class="literal">TIMESTAMP</code> values and Unix timestamp values,
the conversion is lossy because the mapping is not one-to-one
in both directions. For details, see the description of the
<code class="literal">UNIX_TIMESTAMP()</code> function.
</p></li><li><p><a id="function_get-format"></a>
<a id="id2837184" class="indexterm"></a>
<code class="literal">GET_FORMAT(DATE|TIME|DATETIME,
'EUR'|'USA'|'JIS'|'ISO'|'INTERNAL')</code>
</p><p>
Returns a format string. This function is useful in
combination with the <code class="literal">DATE_FORMAT()</code> and the
<code class="literal">STR_TO_DATE()</code> functions.
</p><p>
The possible values for the first and second arguments result
in several possible format strings (for the specifiers used,
see the table in the <code class="literal">DATE_FORMAT()</code> function
description). ISO format refers to ISO 9075, not ISO 8601.
</p><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><tbody><tr><td><span class="bold"><strong>Function Call</strong></span></td><td><span class="bold"><strong>Result</strong></span></td></tr><tr><td><code class="literal">GET_FORMAT(DATE,'USA')</code></td><td><code class="literal">'%m.%d.%Y'</code></td></tr><tr><td><code class="literal">GET_FORMAT(DATE,'JIS')</code></td><td><code class="literal">'%Y-%m-%d'</code></td></tr><tr><td><code class="literal">GET_FORMAT(DATE,'ISO')</code></td><td><code class="literal">'%Y-%m-%d'</code></td></tr><tr><td><code class="literal">GET_FORMAT(DATE,'EUR')</code></td><td><code class="literal">'%d.%m.%Y'</code></td></tr><tr><td><code class="literal">GET_FORMAT(DATE,'INTERNAL')</code></td><td><code class="literal">'%Y%m%d'</code></td></tr><tr><td><code class="literal">GET_FORMAT(DATETIME,'USA')</code></td><td><code class="literal">'%Y-%m-%d %H.%i.%s'</code></td></tr><tr><td><code class="literal">GET_FORMAT(DATETIME,'JIS')</code></td><td><code class="literal">'%Y-%m-%d %H:%i:%s'</code></td></tr><tr><td><code class="literal">GET_FORMAT(DATETIME,'ISO')</code></td><td><code class="literal">'%Y-%m-%d %H:%i:%s'</code></td></tr><tr><td><code class="literal">GET_FORMAT(DATETIME,'EUR')</code></td><td><code class="literal">'%Y-%m-%d %H.%i.%s'</code></td></tr><tr><td><code class="literal">GET_FORMAT(DATETIME,'INTERNAL')</code></td><td><code class="literal">'%Y%m%d%H%i%s'</code></td></tr><tr><td><code class="literal">GET_FORMAT(TIME,'USA')</code></td><td><code class="literal">'%h:%i:%s %p'</code></td></tr><tr><td><code class="literal">GET_FORMAT(TIME,'JIS')</code></td><td><code class="literal">'%H:%i:%s'</code></td></tr><tr><td><code class="literal">GET_FORMAT(TIME,'ISO')</code></td><td><code class="literal">'%H:%i:%s'</code></td></tr><tr><td><code class="literal">GET_FORMAT(TIME,'EUR')</code></td><td><code class="literal">'%H.%i.%s'</code></td></tr><tr><td><code class="literal">GET_FORMAT(TIME,'INTERNAL')</code></td><td><code class="literal">'%H%i%s'</code></td></tr></tbody></table></div><p>
<code class="literal">TIMESTAMP</code> can also be used as the first
argument to <code class="literal">GET_FORMAT()</code>, in which case the
function returns the same values as for
<code class="literal">DATETIME</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT DATE_FORMAT('2003-10-03',GET_FORMAT(DATE,'EUR'));</code></strong>
-> '03.10.2003'
mysql> <strong class="userinput"><code>SELECT STR_TO_DATE('10.31.2003',GET_FORMAT(DATE,'USA'));</code></strong>
-> '2003-10-31'
</pre></li><li><p><a id="function_hour"></a>
<a id="id2837532" class="indexterm"></a>
<code class="literal">HOUR(<em class="replaceable"><code>time</code></em>)</code>
</p><p>
Returns the hour for <em class="replaceable"><code>time</code></em>. The
range of the return value is <code class="literal">0</code> to
<code class="literal">23</code> for time-of-day values. However, the
range of <code class="literal">TIME</code> values actually is much
larger, so <code class="literal">HOUR</code> can return values greater
than <code class="literal">23</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT HOUR('10:05:03');</code></strong>
-> 10
mysql> <strong class="userinput"><code>SELECT HOUR('272:59:59');</code></strong>
-> 272
</pre></li><li><p><a id="function_last-day"></a>
<a id="id2837624" class="indexterm"></a>
<code class="literal">LAST_DAY(<em class="replaceable"><code>date</code></em>)</code>
</p><p>
Takes a date or datetime value and returns the corresponding
value for the last day of the month. Returns
<code class="literal">NULL</code> if the argument is invalid.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT LAST_DAY('2003-02-05');</code></strong>
-> '2003-02-28'
mysql> <strong class="userinput"><code>SELECT LAST_DAY('2004-02-05');</code></strong>
-> '2004-02-29'
mysql> <strong class="userinput"><code>SELECT LAST_DAY('2004-01-01 01:01:01');</code></strong>
-> '2004-01-31'
mysql> <strong class="userinput"><code>SELECT LAST_DAY('2003-03-32');</code></strong>
-> NULL
</pre></li><li><p><a id="function_localtime"></a>
<a id="id2837711" class="indexterm"></a>
<code class="literal">LOCALTIME</code>, <code class="literal">LOCALTIME()</code>
</p><p>
<code class="literal">LOCALTIME</code> and
<code class="literal">LOCALTIME()</code> are synonyms for
<code class="literal">NOW()</code>.
</p></li><li><p><a id="function_localtimestamp"></a>
<a id="id2837774" class="indexterm"></a>
<code class="literal">LOCALTIMESTAMP</code>,
<code class="literal">LOCALTIMESTAMP()</code>
</p><p>
<code class="literal">LOCALTIMESTAMP</code> and
<code class="literal">LOCALTIMESTAMP()</code> are synonyms for
<code class="literal">NOW()</code>.
</p></li><li><p><a id="function_makedate"></a>
<a id="id2837837" class="indexterm"></a>
<code class="literal">MAKEDATE(<em class="replaceable"><code>year</code></em>,<em class="replaceable"><code>dayofyear</code></em>)</code>
</p><p>
Returns a date, given year and day-of-year values.
<em class="replaceable"><code>dayofyear</code></em> must be greater than 0 or
the result is <code class="literal">NULL</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT MAKEDATE(2001,31), MAKEDATE(2001,32);</code></strong>
-> '2001-01-31', '2001-02-01'
mysql> <strong class="userinput"><code>SELECT MAKEDATE(2001,365), MAKEDATE(2004,365);</code></strong>
-> '2001-12-31', '2004-12-30'
mysql> <strong class="userinput"><code>SELECT MAKEDATE(2001,0);</code></strong>
-> NULL
</pre></li><li><p><a id="function_maketime"></a>
<a id="id2837925" class="indexterm"></a>
<code class="literal">MAKETIME(<em class="replaceable"><code>hour</code></em>,<em class="replaceable"><code>minute</code></em>,<em class="replaceable"><code>second</code></em>)</code>
</p><p>
Returns a time value calculated from the
<em class="replaceable"><code>hour</code></em>,
<em class="replaceable"><code>minute</code></em>, and
<em class="replaceable"><code>second</code></em> arguments.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT MAKETIME(12,15,30);</code></strong>
-> '12:15:30'
</pre></li><li><p><a id="function_microsecond"></a>
<a id="id2838008" class="indexterm"></a>
<code class="literal">MICROSECOND(<em class="replaceable"><code>expr</code></em>)</code>
</p><p>
Returns the microseconds from the time or datetime expression
<em class="replaceable"><code>expr</code></em> as a number in the range from
<code class="literal">0</code> to <code class="literal">999999</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT MICROSECOND('12:00:00.123456');</code></strong>
-> 123456
mysql> <strong class="userinput"><code>SELECT MICROSECOND('1997-12-31 23:59:59.000010');</code></strong>
-> 10
</pre></li><li><p><a id="function_minute"></a>
<a id="id2838090" class="indexterm"></a>
<code class="literal">MINUTE(<em class="replaceable"><code>time</code></em>)</code>
</p><p>
Returns the minute for <em class="replaceable"><code>time</code></em>, in the
range <code class="literal">0</code> to <code class="literal">59</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT MINUTE('98-02-03 10:05:03');</code></strong>
-> 5
</pre></li><li><p><a id="function_month"></a>
<a id="id2838165" class="indexterm"></a>
<code class="literal">MONTH(<em class="replaceable"><code>date</code></em>)</code>
</p><p>
Returns the month for <em class="replaceable"><code>date</code></em>, in the
range <code class="literal">0</code> to <code class="literal">12</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT MONTH('1998-02-03');</code></strong>
-> 2
</pre></li><li><p><a id="function_monthname"></a>
<a id="id2838240" class="indexterm"></a>
<code class="literal">MONTHNAME(<em class="replaceable"><code>date</code></em>)</code>
</p><p>
Returns the full name of the month for
<em class="replaceable"><code>date</code></em>. As of MySQL 5.0.25, the
language used for the name is controlled by the value of the
<code class="literal">lc_time_names</code> system variable
(<a href="http://dev.mysql.com/doc/refman/5.0/en/locale-support.html" target="_top">MySQL Server Locale Support</a>).
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT MONTHNAME('1998-02-05');</code></strong>
-> 'February'
</pre></li><li><p><a id="function_now"></a>
<a id="id2838321" class="indexterm"></a>
<code class="literal">NOW()</code>
</p><p>
Returns the current date and time as a value in
<code class="literal">'YYYY-MM-DD HH:MM:SS'</code> or
<code class="literal">YYYYMMDDHHMMSS</code> format, depending on whether
the function is used in a string or numeric context. The value
is expressed in the current time zone.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT NOW();</code></strong>
-> '1997-12-15 23:50:26'
mysql> <strong class="userinput"><code>SELECT NOW() + 0;</code></strong>
-> 19971215235026
</pre><p>
<code class="literal">NOW()</code> returns a constant time that
indicates the time at which the statement began to execute.
(Within a stored routine or trigger, <code class="literal">NOW()</code>
returns the time at which the routine or triggering statement
began to execute.) This differs from the behavior for
<code class="literal">SYSDATE()</code>, which returns the exact time at
which it executes as of MySQL 5.0.13.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT NOW(), SLEEP(2), NOW();</code></strong>
+---------------------+----------+---------------------+
| NOW() | SLEEP(2) | NOW() |
+---------------------+----------+---------------------+
| 2006-04-12 13:47:36 | 0 | 2006-04-12 13:47:36 |
+---------------------+----------+---------------------+
mysql> <strong class="userinput"><code>SELECT SYSDATE(), SLEEP(2), SYSDATE();</code></strong>
+---------------------+----------+---------------------+
| SYSDATE() | SLEEP(2) | SYSDATE() |
+---------------------+----------+---------------------+
| 2006-04-12 13:47:44 | 0 | 2006-04-12 13:47:46 |
+---------------------+----------+---------------------+
</pre><p>
See the description for <code class="literal">SYSDATE()</code> for
additional information about the differences between the two
functions.
</p></li><li><p><a id="function_period-add"></a>
<a id="id2838454" class="indexterm"></a>
<code class="literal">PERIOD_ADD(<em class="replaceable"><code>P</code></em>,<em class="replaceable"><code>N</code></em>)</code>
</p><p>
Adds <em class="replaceable"><code>N</code></em> months to period
<em class="replaceable"><code>P</code></em> (in the format
<code class="literal">YYMM</code> or <code class="literal">YYYYMM</code>). Returns
a value in the format <code class="literal">YYYYMM</code>. Note that the
period argument <em class="replaceable"><code>P</code></em> is
<span class="emphasis"><em>not</em></span> a date value.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT PERIOD_ADD(9801,2);</code></strong>
-> 199803
</pre></li><li><p><a id="function_period-diff"></a>
<a id="id2838549" class="indexterm"></a>
<code class="literal">PERIOD_DIFF(<em class="replaceable"><code>P1</code></em>,<em class="replaceable"><code>P2</code></em>)</code>
</p><p>
Returns the number of months between periods
<em class="replaceable"><code>P1</code></em> and
<em class="replaceable"><code>P2</code></em>. <em class="replaceable"><code>P1</code></em>
and <em class="replaceable"><code>P2</code></em> should be in the format
<code class="literal">YYMM</code> or <code class="literal">YYYYMM</code>. Note
that the period arguments <em class="replaceable"><code>P1</code></em> and
<em class="replaceable"><code>P2</code></em> are <span class="emphasis"><em>not</em></span>
date values.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT PERIOD_DIFF(9802,199703);</code></strong>
-> 11
</pre></li><li><p><a id="function_quarter"></a>
<a id="id2838650" class="indexterm"></a>
<code class="literal">QUARTER(<em class="replaceable"><code>date</code></em>)</code>
</p><p>
Returns the quarter of the year for
<em class="replaceable"><code>date</code></em>, in the range
<code class="literal">1</code> to <code class="literal">4</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT QUARTER('98-04-01');</code></strong>
-> 2
</pre></li><li><p><a id="function_second"></a>
<a id="id2838725" class="indexterm"></a>
<code class="literal">SECOND(<em class="replaceable"><code>time</code></em>)</code>
</p><p>
Returns the second for <em class="replaceable"><code>time</code></em>, in the
range <code class="literal">0</code> to <code class="literal">59</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT SECOND('10:05:03');</code></strong>
-> 3
</pre></li><li><p><a id="function_sec-to-time"></a>
<a id="id2838800" class="indexterm"></a>
<code class="literal">SEC_TO_TIME(<em class="replaceable"><code>seconds</code></em>)</code>
</p><p>
Returns the <em class="replaceable"><code>seconds</code></em> argument,
converted to hours, minutes, and seconds, as a value in
<code class="literal">'HH:MM:SS'</code> or <code class="literal">HHMMSS</code>
format, depending on whether the function is used in a string
or numeric context.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT SEC_TO_TIME(2378);</code></strong>
-> '00:39:38'
mysql> <strong class="userinput"><code>SELECT SEC_TO_TIME(2378) + 0;</code></strong>
-> 3938
</pre></li><li><p><a id="function_str-to-date"></a>
<a id="id2838884" class="indexterm"></a>
<code class="literal">STR_TO_DATE(<em class="replaceable"><code>str</code></em>,<em class="replaceable"><code>format</code></em>)</code>
</p><p>
This is the inverse of the <code class="literal">DATE_FORMAT()</code>
function. It takes a string <em class="replaceable"><code>str</code></em> and
a format string <em class="replaceable"><code>format</code></em>.
<code class="literal">STR_TO_DATE()</code> returns a
<code class="literal">DATETIME</code> value if the format string
contains both date and time parts, or a
<code class="literal">DATE</code> or <code class="literal">TIME</code> value if
the string contains only date or time parts.
</p><p>
The date, time, or datetime values contained in
<em class="replaceable"><code>str</code></em> should be given in the format
indicated by <em class="replaceable"><code>format</code></em>. For the
specifiers that can be used in
<em class="replaceable"><code>format</code></em>, see the
<code class="literal">DATE_FORMAT()</code> function description. If
<em class="replaceable"><code>str</code></em> contains an illegal date, time,
or datetime value, <code class="literal">STR_TO_DATE()</code> returns
<code class="literal">NULL</code>. Starting from MySQL 5.0.3, an illegal
value also produces a warning.
</p><p>
Range checking on the parts of date values is as described in
<a href="http://dev.mysql.com/doc/refman/5.0/en/datetime.html" target="_top">The <code class="literal">DATETIME</code>, <code class="literal">DATE</code>, and <code class="literal">TIMESTAMP</code> Types</a>. This means, for example, that
“<span class="quote">zero</span>” dates or dates with part values of 0 are
allowed unless the SQL mode is set to disallow such values.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT STR_TO_DATE('00/00/0000', '%m/%d/%Y');</code></strong>
-> '0000-00-00'
mysql> <strong class="userinput"><code>SELECT STR_TO_DATE('04/31/2004', '%m/%d/%Y');</code></strong>
-> '2004-04-31'
</pre><p>
<span class="bold"><strong>Note</strong></span>: You cannot use format
<code class="literal">"%X%V"</code> to convert a year-week string to a
date because the combination of a year and week does not
uniquely identify a year and month if the week crosses a month
boundary. To convert a year-week to a date, then you should
also specify the weekday:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT STR_TO_DATE('200442 Monday', '%X%V %W');</code></strong>
-> '2004-10-18'
</pre></li><li><p><a id="function_subdate"></a>
<a id="id2839101" class="indexterm"></a>
<code class="literal">SUBDATE(<em class="replaceable"><code>date</code></em>,INTERVAL
<em class="replaceable"><code>expr</code></em>
<em class="replaceable"><code>unit</code></em>)</code>,
<code class="literal">SUBDATE(<em class="replaceable"><code>expr</code></em>,<em class="replaceable"><code>days</code></em>)</code>
</p><p>
When invoked with the <code class="literal">INTERVAL</code> form of the
second argument, <code class="literal">SUBDATE()</code> is a synonym for
<code class="literal">DATE_SUB()</code>. For information on the
<code class="literal">INTERVAL</code> <em class="replaceable"><code>unit</code></em>
argument, see the discussion for
<code class="literal">DATE_ADD()</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT DATE_SUB('1998-01-02', INTERVAL 31 DAY);</code></strong>
-> '1997-12-02'
mysql> <strong class="userinput"><code>SELECT SUBDATE('1998-01-02', INTERVAL 31 DAY);</code></strong>
-> '1997-12-02'
</pre><p>
The second form allows the use of an integer value for
<em class="replaceable"><code>days</code></em>. In such cases, it is
interpreted as the number of days to be subtracted from the
date or datetime expression <em class="replaceable"><code>expr</code></em>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT SUBDATE('1998-01-02 12:00:00', 31);</code></strong>
-> '1997-12-02 12:00:00'
</pre></li><li><p><a id="function_subtime"></a>
<a id="id2839233" class="indexterm"></a>
<code class="literal">SUBTIME(<em class="replaceable"><code>expr1</code></em>,<em class="replaceable"><code>expr2</code></em>)</code>
</p><p>
<code class="literal">SUBTIME()</code> returns
<em class="replaceable"><code>expr1</code></em> –
<em class="replaceable"><code>expr2</code></em> expressed as a value in the
same format as <em class="replaceable"><code>expr1</code></em>.
<em class="replaceable"><code>expr1</code></em> is a time or datetime
expression, and <em class="replaceable"><code>expr2</code></em> is a time
expression.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT SUBTIME('1997-12-31 23:59:59.999999','1 1:1:1.000002');</code></strong>
-> '1997-12-30 22:58:58.999997'
mysql> <strong class="userinput"><code>SELECT SUBTIME('01:00:00.999999', '02:00:00.999998');</code></strong>
-> '-00:59:59.999999'
</pre></li><li><p><a id="function_sysdate"></a>
<a id="id2839340" class="indexterm"></a>
<code class="literal">SYSDATE()</code>
</p><p>
Returns the current date and time as a value in
<code class="literal">'YYYY-MM-DD HH:MM:SS'</code> or
<code class="literal">YYYYMMDDHHMMSS</code> format, depending on whether
the function is used in a string or numeric context.
</p><p>
As of MySQL 5.0.13, <code class="literal">SYSDATE()</code> returns the
time at which it executes. This differs from the behavior for
<code class="literal">NOW()</code>, which returns a constant time that
indicates the time at which the statement began to execute.
(Within a stored routine or trigger, <code class="literal">NOW()</code>
returns the time at which the routine or triggering statement
began to execute.)
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT NOW(), SLEEP(2), NOW();</code></strong>
+---------------------+----------+---------------------+
| NOW() | SLEEP(2) | NOW() |
+---------------------+----------+---------------------+
| 2006-04-12 13:47:36 | 0 | 2006-04-12 13:47:36 |
+---------------------+----------+---------------------+
mysql> <strong class="userinput"><code>SELECT SYSDATE(), SLEEP(2), SYSDATE();</code></strong>
+---------------------+----------+---------------------+
| SYSDATE() | SLEEP(2) | SYSDATE() |
+---------------------+----------+---------------------+
| 2006-04-12 13:47:44 | 0 | 2006-04-12 13:47:46 |
+---------------------+----------+---------------------+
</pre><p>
In addition, the <code class="literal">SET TIMESTAMP</code> statement
affects the value returned by <code class="literal">NOW()</code> but not
by <code class="literal">SYSDATE()</code>. This means that timestamp
settings in the binary log have no effect on invocations of
<code class="literal">SYSDATE()</code>.
</p><p>
Because <code class="literal">SYSDATE()</code> can return different
values even within the same statement, and is not affected by
<code class="literal">SET TIMESTAMP</code>, it is non-deterministic and
therefore unsafe for replication. If that is a problem, you
can start the server with the
<code class="option">--sysdate-is-now</code> option to cause
<code class="literal">SYSDATE()</code> to be an alias for
<code class="literal">NOW()</code>.
</p></li><li><p><a id="function_time"></a>
<a id="id2839500" class="indexterm"></a>
<code class="literal">TIME(<em class="replaceable"><code>expr</code></em>)</code>
</p><p>
Extracts the time part of the time or datetime expression
<em class="replaceable"><code>expr</code></em> and returns it as a string.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT TIME('2003-12-31 01:02:03');</code></strong>
-> '01:02:03'
mysql> <strong class="userinput"><code>SELECT TIME('2003-12-31 01:02:03.000123');</code></strong>
-> '01:02:03.000123'
</pre></li><li><p><a id="function_timediff"></a>
<a id="id2839575" class="indexterm"></a>
<code class="literal">TIMEDIFF(<em class="replaceable"><code>expr1</code></em>,<em class="replaceable"><code>expr2</code></em>)</code>
</p><p>
<code class="literal">TIMEDIFF()</code> returns
<em class="replaceable"><code>expr1</code></em> –
<em class="replaceable"><code>expr2</code></em> expressed as a time value.
<em class="replaceable"><code>expr1</code></em> and
<em class="replaceable"><code>expr2</code></em> are time or date-and-time
expressions, but both must be of the same type.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT TIMEDIFF('2000:01:01 00:00:00',</code></strong>
-> <strong class="userinput"><code>'2000:01:01 00:00:00.000001');</code></strong>
-> '-00:00:00.000001'
mysql> <strong class="userinput"><code>SELECT TIMEDIFF('1997-12-31 23:59:59.000001',</code></strong>
-> <strong class="userinput"><code>'1997-12-30 01:01:01.000002');</code></strong>
-> '46:58:57.999999'
</pre></li><li><p><a id="function_timestamp"></a>
<a id="id2839693" class="indexterm"></a>
<code class="literal">TIMESTAMP(<em class="replaceable"><code>expr</code></em>)</code>,
<code class="literal">TIMESTAMP(<em class="replaceable"><code>expr1</code></em>,<em class="replaceable"><code>expr2</code></em>)</code>
</p><p>
With a single argument, this function returns the date or
datetime expression <em class="replaceable"><code>expr</code></em> as a
datetime value. With two arguments, it adds the time
expression <em class="replaceable"><code>expr2</code></em> to the date or
datetime expression <em class="replaceable"><code>expr1</code></em> and
returns the result as a datetime value.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT TIMESTAMP('2003-12-31');</code></strong>
-> '2003-12-31 00:00:00'
mysql> <strong class="userinput"><code>SELECT TIMESTAMP('2003-12-31 12:00:00','12:00:00');</code></strong>
-> '2004-01-01 00:00:00'
</pre></li><li><p><a id="function_timestampadd"></a>
<a id="id2839791" class="indexterm"></a>
<code class="literal">TIMESTAMPADD(<em class="replaceable"><code>unit</code></em>,<em class="replaceable"><code>interval</code></em>,<em class="replaceable"><code>datetime_expr</code></em>)</code>
</p><p>
Adds the integer expression
<em class="replaceable"><code>interval</code></em> to the date or datetime
expression <em class="replaceable"><code>datetime_expr</code></em>. The unit
for <em class="replaceable"><code>interval</code></em> is given by the
<em class="replaceable"><code>unit</code></em> argument, which should be one
of the following values: <code class="literal">FRAC_SECOND</code>,
<code class="literal">SECOND</code>, <code class="literal">MINUTE</code>,
<code class="literal">HOUR</code>, <code class="literal">DAY</code>,
<code class="literal">WEEK</code>, <code class="literal">MONTH</code>,
<code class="literal">QUARTER</code>, or <code class="literal">YEAR</code>.
</p><p>
The <em class="replaceable"><code>unit</code></em> value may be specified
using one of keywords as shown, or with a prefix of
<code class="literal">SQL_TSI_</code>. For example,
<code class="literal">DAY</code> and <code class="literal">SQL_TSI_DAY</code> both
are legal.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT TIMESTAMPADD(MINUTE,1,'2003-01-02');</code></strong>
-> '2003-01-02 00:01:00'
mysql> <strong class="userinput"><code>SELECT TIMESTAMPADD(WEEK,1,'2003-01-02');</code></strong>
-> '2003-01-09'
</pre><p>
<code class="literal">TIMESTAMPADD()</code> is available as of MySQL
5.0.0.
</p></li><li><p><a id="function_timestampdiff"></a>
<a id="id2839948" class="indexterm"></a>
<code class="literal">TIMESTAMPDIFF(<em class="replaceable"><code>unit</code></em>,<em class="replaceable"><code>datetime_expr1</code></em>,<em class="replaceable"><code>datetime_expr2</code></em>)</code>
</p><p>
Returns the integer difference between the date or datetime
expressions <em class="replaceable"><code>datetime_expr1</code></em> and
<em class="replaceable"><code>datetime_expr2</code></em>. The unit for the
result is given by the <em class="replaceable"><code>unit</code></em>
argument. The legal values for <em class="replaceable"><code>unit</code></em>
are the same as those listed in the description of the
<code class="literal">TIMESTAMPADD()</code> function.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01');</code></strong>
-> 3
mysql> <strong class="userinput"><code>SELECT TIMESTAMPDIFF(YEAR,'2002-05-01','2001-01-01');</code></strong>
-> -1
</pre><p>
<code class="literal">TIMESTAMPDIFF()</code> is available as of MySQL
5.0.0.
</p></li><li><p><a id="function_time-format"></a>
<a id="id2840055" class="indexterm"></a>
<code class="literal">TIME_FORMAT(<em class="replaceable"><code>time</code></em>,<em class="replaceable"><code>format</code></em>)</code>
</p><p>
This is used like the <code class="literal">DATE_FORMAT()</code>
function, but the <em class="replaceable"><code>format</code></em> string may
contain format specifiers only for hours, minutes, and
seconds. Other specifiers produce a <code class="literal">NULL</code>
value or <code class="literal">0</code>.
</p><p>
If the <em class="replaceable"><code>time</code></em> value contains an hour
part that is greater than <code class="literal">23</code>, the
<code class="literal">%H</code> and <code class="literal">%k</code> hour format
specifiers produce a value larger than the usual range of
<code class="literal">0..23</code>. The other hour format specifiers
produce the hour value modulo 12.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT TIME_FORMAT('100:00:00', '%H %k %h %I %l');</code></strong>
-> '100 100 04 04 4'
</pre></li><li><p><a id="function_time-to-sec"></a>
<a id="id2840167" class="indexterm"></a>
<code class="literal">TIME_TO_SEC(<em class="replaceable"><code>time</code></em>)</code>
</p><p>
Returns the <em class="replaceable"><code>time</code></em> argument,
converted to seconds.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT TIME_TO_SEC('22:23:00');</code></strong>
-> 80580
mysql> <strong class="userinput"><code>SELECT TIME_TO_SEC('00:39:38');</code></strong>
-> 2378
</pre></li><li><p><a id="function_to-days"></a>
<a id="id2840240" class="indexterm"></a>
<code class="literal">TO_DAYS(<em class="replaceable"><code>date</code></em>)</code>
</p><p>
Given a date <em class="replaceable"><code>date</code></em>, returns a day
number (the number of days since year 0).
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT TO_DAYS(950501);</code></strong>
-> 728779
mysql> <strong class="userinput"><code>SELECT TO_DAYS('1997-10-07');</code></strong>
-> 729669
</pre><p>
<code class="literal">TO_DAYS()</code> is not intended for use with
values that precede the advent of the Gregorian calendar
(1582), because it does not take into account the days that
were lost when the calendar was changed. For dates before 1582
(and possibly a later year in other locales), results from
this function are not reliable. See
<a href="mysqlqb_functions.html#mysql-calendar" title="What Calendar Is Used By MySQL?">What Calendar Is Used By MySQL?</a>, for details.
</p><p>
Remember that MySQL converts two-digit year values in dates to
four-digit form using the rules in
<a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-types.html" target="_top">Date and Time Types</a>. For example,
<code class="literal">'1997-10-07'</code> and
<code class="literal">'97-10-07'</code> are seen as identical dates:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT TO_DAYS('1997-10-07'), TO_DAYS('97-10-07');</code></strong>
-> 729669, 729669
</pre></li><li><p><a id="function_unix-timestamp"></a>
<a id="id2840362" class="indexterm"></a>
<code class="literal">UNIX_TIMESTAMP()</code>,
<code class="literal">UNIX_TIMESTAMP(<em class="replaceable"><code>date</code></em>)</code>
</p><p>
If called with no argument, returns a Unix timestamp (seconds
since <code class="literal">'1970-01-01 00:00:00'</code> UTC) as an
unsigned integer. If <code class="literal">UNIX_TIMESTAMP()</code> is
called with a <em class="replaceable"><code>date</code></em> argument, it
returns the value of the argument as seconds since
<code class="literal">'1970-01-01 00:00:00'</code> UTC.
<em class="replaceable"><code>date</code></em> may be a
<code class="literal">DATE</code> string, a <code class="literal">DATETIME</code>
string, a <code class="literal">TIMESTAMP</code>, or a number in the
format <code class="literal">YYMMDD</code> or
<code class="literal">YYYYMMDD</code>. The server interprets
<em class="replaceable"><code>date</code></em> as a value in the current time
zone and converts it to an internal value in UTC. Clients can
set their time zone as described in
<a href="http://dev.mysql.com/doc/refman/5.0/en/time-zone-support.html" target="_top">MySQL Server Time Zone Support</a>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT UNIX_TIMESTAMP();</code></strong>
-> 882226357
mysql> <strong class="userinput"><code>SELECT UNIX_TIMESTAMP('1997-10-04 22:23:00');</code></strong>
-> 875996580
</pre><p>
When <code class="literal">UNIX_TIMESTAMP</code> is used on a
<code class="literal">TIMESTAMP</code> column, the function returns the
internal timestamp value directly, with no implicit
“<span class="quote">string-to-Unix-timestamp</span>” conversion. If you
pass an out-of-range date to
<code class="literal">UNIX_TIMESTAMP()</code>, it returns
<code class="literal">0</code>.
</p><p>
Note: If you use <code class="literal">UNIX_TIMESTAMP()</code> and
<code class="literal">FROM_UNIXTIME()</code> to convert between
<code class="literal">TIMESTAMP</code> values and Unix timestamp values,
the conversion is lossy because the mapping is not one-to-one
in both directions. For example, due to conventions for local
time zone changes, it is possible for two
<code class="literal">UNIX_TIMESTAMP()</code> to map two
<code class="literal">TIMESTAMP</code> values to the same Unix timestamp
value. <code class="literal">FROM_UNIXTIME()</code> will map that value
back to only one of the original <code class="literal">TIMESTAMP</code>
values. Here is an example, using <code class="literal">TIMESTAMP</code>
values in the <code class="literal">CET</code> time zone:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT UNIX_TIMESTAMP('2005-03-27 03:00:00');</code></strong>
+---------------------------------------+
| UNIX_TIMESTAMP('2005-03-27 03:00:00') |
+---------------------------------------+
| 1111885200 |
+---------------------------------------+
mysql> <strong class="userinput"><code>SELECT UNIX_TIMESTAMP('2005-03-27 02:00:00');</code></strong>
+---------------------------------------+
| UNIX_TIMESTAMP('2005-03-27 02:00:00') |
+---------------------------------------+
| 1111885200 |
+---------------------------------------+
mysql> <strong class="userinput"><code>SELECT FROM_UNIXTIME(1111885200);</code></strong>
+---------------------------+
| FROM_UNIXTIME(1111885200) |
+---------------------------+
| 2005-03-27 03:00:00 |
+---------------------------+
</pre><p>
If you want to subtract <code class="literal">UNIX_TIMESTAMP()</code>
columns, you might want to cast the result to signed integers.
See <a href="mysqlqb_functions.html#cast-functions" title="Cast Functions and Operators">Cast Functions and Operators</a>.
</p></li><li><p><a id="function_utc-date"></a>
<a id="id2840611" class="indexterm"></a>
<code class="literal">UTC_DATE</code>, <code class="literal">UTC_DATE()</code>
</p><p>
Returns the current UTC date as a value in
<code class="literal">'YYYY-MM-DD'</code> or <code class="literal">YYYYMMDD</code>
format, depending on whether the function is used in a string
or numeric context.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT UTC_DATE(), UTC_DATE() + 0;</code></strong>
-> '2003-08-14', 20030814
</pre></li><li><p><a id="function_utc-time"></a>
<a id="id2840686" class="indexterm"></a>
<code class="literal">UTC_TIME</code>, <code class="literal">UTC_TIME()</code>
</p><p>
Returns the current UTC time as a value in
<code class="literal">'HH:MM:SS'</code> or <code class="literal">HHMMSS</code>
format, depending on whether the function is used in a string
or numeric context.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT UTC_TIME(), UTC_TIME() + 0;</code></strong>
-> '18:07:53', 180753
</pre></li><li><p><a id="function_utc-timestamp"></a>
<a id="id2840760" class="indexterm"></a>
<code class="literal">UTC_TIMESTAMP</code>,
<code class="literal">UTC_TIMESTAMP()</code>
</p><p>
Returns the current UTC date and time as a value in
<code class="literal">'YYYY-MM-DD HH:MM:SS'</code> or
<code class="literal">YYYYMMDDHHMMSS</code> format, depending on whether
the function is used in a string or numeric context.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT UTC_TIMESTAMP(), UTC_TIMESTAMP() + 0;</code></strong>
-> '2003-08-14 18:08:04', 20030814180804
</pre></li><li><p><a id="function_week"></a>
<a id="id2840837" class="indexterm"></a>
<code class="literal">WEEK(<em class="replaceable"><code>date</code></em>[,<em class="replaceable"><code>mode</code></em>])</code>
</p><p>
This function returns the week number for
<em class="replaceable"><code>date</code></em>. The two-argument form of
<code class="literal">WEEK()</code> allows you to specify whether the
week starts on Sunday or Monday and whether the return value
should be in the range from <code class="literal">0</code> to
<code class="literal">53</code> or from <code class="literal">1</code> to
<code class="literal">53</code>. If the <em class="replaceable"><code>mode</code></em>
argument is omitted, the value of the
<code class="literal">default_week_format</code> system variable is
used. See <a href="http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html" target="_top">System Variables</a>.
</p><p>
The following table describes how the
<em class="replaceable"><code>mode</code></em> argument works.
</p><div class="informaltable"><table border="1"><colgroup><col /><col /><col /><col /></colgroup><tbody><tr><td> </td><td><span class="bold"><strong>First day</strong></span></td><td> </td><td> </td></tr><tr><td><span class="bold"><strong>Mode</strong></span></td><td><span class="bold"><strong>of week</strong></span></td><td><span class="bold"><strong>Range</strong></span></td><td><span class="bold"><strong>Week 1 is the first week …</strong></span></td></tr><tr><td>0</td><td>Sunday</td><td>0-53</td><td>with a Sunday in this year</td></tr><tr><td>1</td><td>Monday</td><td>0-53</td><td>with more than 3 days this year</td></tr><tr><td>2</td><td>Sunday</td><td>1-53</td><td>with a Sunday in this year</td></tr><tr><td>3</td><td>Monday</td><td>1-53</td><td>with more than 3 days this year</td></tr><tr><td>4</td><td>Sunday</td><td>0-53</td><td>with more than 3 days this year</td></tr><tr><td>5</td><td>Monday</td><td>0-53</td><td>with a Monday in this year</td></tr><tr><td>6</td><td>Sunday</td><td>1-53</td><td>with more than 3 days this year</td></tr><tr><td>7</td><td>Monday</td><td>1-53</td><td>with a Monday in this year</td></tr></tbody></table></div><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT WEEK('1998-02-20');</code></strong>
-> 7
mysql> <strong class="userinput"><code>SELECT WEEK('1998-02-20',0);</code></strong>
-> 7
mysql> <strong class="userinput"><code>SELECT WEEK('1998-02-20',1);</code></strong>
-> 8
mysql> <strong class="userinput"><code>SELECT WEEK('1998-12-31',1);</code></strong>
-> 53
</pre><p>
Note that if a date falls in the last week of the previous
year, MySQL returns <code class="literal">0</code> if you do not use
<code class="literal">2</code>, <code class="literal">3</code>,
<code class="literal">6</code>, or <code class="literal">7</code> as the optional
<em class="replaceable"><code>mode</code></em> argument:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT YEAR('2000-01-01'), WEEK('2000-01-01',0);</code></strong>
-> 2000, 0
</pre><p>
One might argue that MySQL should return <code class="literal">52</code>
for the <code class="literal">WEEK()</code> function, because the given
date actually occurs in the 52nd week of 1999. We decided to
return <code class="literal">0</code> instead because we want the
function to return “<span class="quote">the week number in the given
year.</span>” This makes use of the <code class="literal">WEEK()</code>
function reliable when combined with other functions that
extract a date part from a date.
</p><p>
If you would prefer the result to be evaluated with respect to
the year that contains the first day of the week for the given
date, use <code class="literal">0</code>, <code class="literal">2</code>,
<code class="literal">5</code>, or <code class="literal">7</code> as the optional
<em class="replaceable"><code>mode</code></em> argument.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT WEEK('2000-01-01',2);</code></strong>
-> 52
</pre><p>
Alternatively, use the <code class="literal">YEARWEEK()</code> function:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT YEARWEEK('2000-01-01');</code></strong>
-> 199952
mysql> <strong class="userinput"><code>SELECT MID(YEARWEEK('2000-01-01'),5,2);</code></strong>
-> '52'
</pre></li><li><p><a id="function_weekday"></a>
<a id="id2841296" class="indexterm"></a>
<code class="literal">WEEKDAY(<em class="replaceable"><code>date</code></em>)</code>
</p><p>
Returns the weekday index for <em class="replaceable"><code>date</code></em>
(<code class="literal">0</code> = Monday, <code class="literal">1</code> =
Tuesday, … <code class="literal">6</code> = Sunday).
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT WEEKDAY('1998-02-03 22:23:00');</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT WEEKDAY('1997-11-05');</code></strong>
-> 2
</pre></li><li><p><a id="function_weekofyear"></a>
<a id="id2841380" class="indexterm"></a>
<code class="literal">WEEKOFYEAR(<em class="replaceable"><code>date</code></em>)</code>
</p><p>
Returns the calendar week of the date as a number in the range
from <code class="literal">1</code> to <code class="literal">53</code>.
<code class="literal">WEEKOFYEAR()</code> is a compatibility function
that is equivalent to
<code class="literal">WEEK(<em class="replaceable"><code>date</code></em>,3)</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT WEEKOFYEAR('1998-02-20');</code></strong>
-> 8
</pre></li><li><p><a id="function_year"></a>
<a id="id2841464" class="indexterm"></a>
<code class="literal">YEAR(<em class="replaceable"><code>date</code></em>)</code>
</p><p>
Returns the year for <em class="replaceable"><code>date</code></em>, in the
range <code class="literal">1000</code> to <code class="literal">9999</code>, or
<code class="literal">0</code> for the “<span class="quote">zero</span>” date.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT YEAR('98-02-03');</code></strong>
-> 1998
</pre></li><li><p><a id="function_yearweek"></a>
<a id="id2841547" class="indexterm"></a>
<code class="literal">YEARWEEK(<em class="replaceable"><code>date</code></em>)</code>,
<code class="literal">YEARWEEK(<em class="replaceable"><code>date</code></em>,<em class="replaceable"><code>mode</code></em>)</code>
</p><p>
Returns year and week for a date. The
<em class="replaceable"><code>mode</code></em> argument works exactly like
the <em class="replaceable"><code>mode</code></em> argument to
<code class="literal">WEEK()</code>. The year in the result may be
different from the year in the date argument for the first and
the last week of the year.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT YEARWEEK('1987-01-01');</code></strong>
-> 198653
</pre><p>
Note that the week number is different from what the
<code class="literal">WEEK()</code> function would return
(<code class="literal">0</code>) for optional arguments
<code class="literal">0</code> or <code class="literal">1</code>, as
<code class="literal">WEEK()</code> then returns the week in the context
of the given year.
</p></li></ul></div></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="mysql-calendar"></a>What Calendar Is Used By MySQL?</h2></div></div></div><a id="id2841654" class="indexterm"></a><p>
MySQL uses what is known as a <em class="firstterm">proleptic Gregorian
calendar</em>.
</p><p>
Every country that has switched from the Julian to the Gregorian
calendar has had to discard at least ten days during the switch.
To see how this works, consider the month of October 1582, when
the first Julian-to-Gregorian switch occurred:
</p><div class="informaltable"><table border="1"><colgroup><col /><col /><col /><col /><col /><col /><col /></colgroup><tbody><tr><td>Monday</td><td>Tuesday</td><td>Wednesday</td><td>Thursday</td><td>Friday</td><td>Saturday</td><td>Sunday</td></tr><tr><td>1</td><td>2</td><td>3</td><td>4</td><td>15</td><td>16</td><td>17</td></tr><tr><td>18</td><td>19</td><td>20</td><td>21</td><td>22</td><td>23</td><td>24</td></tr><tr><td>25</td><td>26</td><td>27</td><td>28</td><td>29</td><td>30</td><td>31</td></tr></tbody></table></div><p>
There are no dates between October 4 and October 15. This
discontinuity is called the <em class="firstterm">cutover</em>. Any
dates before the cutover are Julian, and any dates following the
cutover are Gregorian. Dates during a cutover are non-existent.
</p><p>
A calendar applied to dates when it wasn't actually in use is
called <em class="firstterm">proleptic</em>. Thus, if we assume there
was never a cutover and Gregorian rules always rule, we have a
proleptic Gregorian calendar. This is what is used by MySQL, as is
required by standard SQL. For this reason, dates prior to the
cutover stored as MySQL <code class="literal">DATE</code> or
<code class="literal">DATETIME</code> values must be adjusted to compensate
for the difference. It is important to realize that the cutover
did not occur at the same time in all countries, and that the
later it happened, the more days were lost. For example, in Great
Britain, it took place in 1752, when Wednesday September 2 was
followed by Thursday September 14. Russia remained on the Julian
calendar until 1918, losing 13 days in the process, and what is
popularly referred to as its “<span class="quote">October Revolution</span>”
occurred in November according to the Gregorian calendar.
</p></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="fulltext-search"></a>Full-Text Search Functions</h2></div></div></div><div class="toc"><dl><dt><span class="section"><a href="mysqlqb_functions.html#fulltext-boolean">Boolean Full-Text Searches</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#fulltext-query-expansion">Full-Text Searches with Query Expansion</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#fulltext-stopwords">Full-Text Stopwords</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#fulltext-restrictions">Full-Text Restrictions</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#fulltext-fine-tuning">Fine-Tuning MySQL Full-Text Search</a></span></dt></dl></div><a id="id2841890" class="indexterm"></a><a id="id2841900" class="indexterm"></a><a id="id2841907" class="indexterm"></a><a id="id2841914" class="indexterm"></a><pre class="programlisting">MATCH (<em class="replaceable"><code>col1</code></em>,<em class="replaceable"><code>col2</code></em>,...) AGAINST (<em class="replaceable"><code>expr</code></em> [<em class="replaceable"><code>search_modifier</code></em>])
<em class="replaceable"><code>search_modifier:</code></em> { IN BOOLEAN MODE | WITH QUERY EXPANSION }
</pre><p>
MySQL has support for full-text indexing and searching:
</p><div class="itemizedlist"><ul type="disc"><li><p>
A full-text index in MySQL is an index of type
<code class="literal">FULLTEXT</code>.
</p></li><li><p>
Full-text indexes can be used only with
<code class="literal">MyISAM</code> tables, and can be created only for
<code class="literal">CHAR</code>, <code class="literal">VARCHAR</code>, or
<code class="literal">TEXT</code> columns.
</p></li><li><p>
A <code class="literal">FULLTEXT</code> index definition can be given in
the <code class="literal">CREATE TABLE</code> statement when a table is
created, or added later using <code class="literal">ALTER TABLE</code>
or <code class="literal">CREATE INDEX</code>.
</p></li><li><p>
For large datasets, it is much faster to load your data into a
table that has no <code class="literal">FULLTEXT</code> index and then
create the index after that, than to load data into a table
that has an existing <code class="literal">FULLTEXT</code> index.
</p></li></ul></div><p>
Full-text searching is performed using <code class="literal">MATCH() ...
AGAINST</code> syntax. <code class="literal">MATCH()</code> takes a
comma-separated list that names the columns to be searched.
<code class="literal">AGAINST</code> takes a string to search for, and an
optional modifier that indicates what type of search to perform.
The search string must be a literal string, not a variable or a
column name. There are three types of full-text searches:
</p><div class="itemizedlist"><ul type="disc"><li><p>
A boolean search interprets the search string using the rules
of a special query language. The string contains the words to
search for. It can also contain operators that specify
requirements such that a word must be present or absent in
matching rows, or that it should be weighted higher or lower
than usual. Common words such as “<span class="quote">some</span>” or
“<span class="quote">then</span>” are stopwords and do not match if present
in the search string. The <code class="literal">IN BOOLEAN MODE</code>
modifier specifies a boolean search. For more information, see
<a href="mysqlqb_functions.html#fulltext-boolean" title="Boolean Full-Text Searches">Boolean Full-Text Searches</a>.
</p></li><li><p>
A natural language search interprets the search string as a
phrase in natural human language (a phrase in free text).
There are no special operators. The stopword list applies. In
addition, words that are present in more than 50% of the rows
are considered common and do not match. Full-text searches are
natural language searches if no modifier is given.
</p></li><li><p>
A query expansion search is a modification of a natural
language search. The search string is used to perform a
natural language search. Then words from the most relevant
rows returned by the search are added to the search string and
the search is done again. The query returns the rows from the
second search. The <code class="literal">WITH QUERY EXPANSION</code>
modifier specifies a query expansion search. For more
information, see <a href="mysqlqb_functions.html#fulltext-query-expansion" title="Full-Text Searches with Query Expansion">Full-Text Searches with Query Expansion</a>.
</p></li></ul></div><p>
Constraints on full-text searching are listed in
<a href="mysqlqb_functions.html#fulltext-restrictions" title="Full-Text Restrictions">Full-Text Restrictions</a>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>CREATE TABLE articles (</code></strong>
-> <strong class="userinput"><code>id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,</code></strong>
-> <strong class="userinput"><code>title VARCHAR(200),</code></strong>
-> <strong class="userinput"><code>body TEXT,</code></strong>
-> <strong class="userinput"><code>FULLTEXT (title,body)</code></strong>
-> <strong class="userinput"><code>);</code></strong>
Query OK, 0 rows affected (0.00 sec)
mysql> <strong class="userinput"><code>INSERT INTO articles (title,body) VALUES</code></strong>
-> <strong class="userinput"><code>('MySQL Tutorial','DBMS stands for DataBase ...'),</code></strong>
-> <strong class="userinput"><code>('How To Use MySQL Well','After you went through a ...'),</code></strong>
-> <strong class="userinput"><code>('Optimizing MySQL','In this tutorial we will show ...'),</code></strong>
-> <strong class="userinput"><code>('1001 MySQL Tricks','1. Never run mysqld as root. 2. ...'),</code></strong>
-> <strong class="userinput"><code>('MySQL vs. YourSQL','In the following database comparison ...'),</code></strong>
-> <strong class="userinput"><code>('MySQL Security','When configured properly, MySQL ...');</code></strong>
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> <strong class="userinput"><code>SELECT * FROM articles</code></strong>
-> <strong class="userinput"><code>WHERE MATCH (title,body) AGAINST ('database');</code></strong>
+----+-------------------+------------------------------------------+
| id | title | body |
+----+-------------------+------------------------------------------+
| 5 | MySQL vs. YourSQL | In the following database comparison ... |
| 1 | MySQL Tutorial | DBMS stands for DataBase ... |
+----+-------------------+------------------------------------------+
2 rows in set (0.00 sec)
</pre><p>
The <code class="literal">MATCH()</code> function performs a natural
language search for a string against a <em class="firstterm">text
collection</em>. A collection is a set of one or more
columns included in a <code class="literal">FULLTEXT</code> index. The
search string is given as the argument to
<code class="literal">AGAINST()</code>. For each row in the table,
<code class="literal">MATCH()</code> returns a relevance value; that is, a
similarity measure between the search string and the text in that
row in the columns named in the <code class="literal">MATCH()</code> list.
</p><p>
By default, the search is performed in case-insensitive fashion.
However, you can perform a case-sensitive full-text search by
using a binary collation for the indexed columns. For example, a
column that uses the <code class="literal">latin1</code> character set of
can be assigned a collation of <code class="literal">latin1_bin</code> to
make it case sensitive for full-text searches.
</p><p>
When <code class="literal">MATCH()</code> is used in a
<code class="literal">WHERE</code> clause, as in the example shown earlier,
the rows returned are automatically sorted with the highest
relevance first. Relevance values are non-negative floating-point
numbers. Zero relevance means no similarity. Relevance is computed
based on the number of words in the row, the number of unique
words in that row, the total number of words in the collection,
and the number of documents (rows) that contain a particular word.
</p><p>
To simply count matches, you could use a query like this:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT COUNT(*) FROM articles</code></strong>
-> <strong class="userinput"><code>WHERE MATCH (title,body)</code></strong>
-> <strong class="userinput"><code>AGAINST ('database');</code></strong>
+----------+
| COUNT(*) |
+----------+
| 2 |
+----------+
1 row in set (0.00 sec)
</pre><p>
However, you might find it quicker to rewrite the query as
follows:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT</code></strong>
-> <strong class="userinput"><code>COUNT(IF(MATCH (title,body) AGAINST ('database'), 1, NULL))</code></strong>
-> <strong class="userinput"><code>AS count</code></strong>
-> <strong class="userinput"><code>FROM articles;</code></strong>
+-------+
| count |
+-------+
| 2 |
+-------+
1 row in set (0.00 sec)
</pre><p>
The first query sorts the results by relevance whereas the second
does not. However, the second query performs a full table scan and
the first does not. The first may be faster if the search matches
few rows; otherwise, the second may be faster because it would
read many rows anyway.
</p><p>
For natural-language full-text searches, it is a requirement that
the columns named in the <code class="literal">MATCH()</code> function be
the same columns included in some <code class="literal">FULLTEXT</code>
index in your table. For the preceding query, note that the
columns named in the <code class="literal">MATCH()</code> function
(<code class="literal">title</code> and <code class="literal">body</code>) are the
same as those named in the definition of the
<code class="literal">article</code> table's <code class="literal">FULLTEXT</code>
index. If you wanted to search the <code class="literal">title</code> or
<code class="literal">body</code> separately, you would need to create
separate <code class="literal">FULLTEXT</code> indexes for each column.
</p><p>
It is also possible to perform a boolean search or a search with
query expansion. These search types are described in
<a href="mysqlqb_functions.html#fulltext-boolean" title="Boolean Full-Text Searches">Boolean Full-Text Searches</a>, and
<a href="mysqlqb_functions.html#fulltext-query-expansion" title="Full-Text Searches with Query Expansion">Full-Text Searches with Query Expansion</a>.
</p><p>
A full-text search that uses an index can name columns only from a
single table in the <code class="literal">MATCH()</code> clause because an
index cannot span multiple tables. A boolean search can be done in
the absence of an index (albeit more slowly), in which case it is
possible to name columns from multiple tables.
</p><p>
The preceding example is a basic illustration that shows how to
use the <code class="literal">MATCH()</code> function where rows are
returned in order of decreasing relevance. The next example shows
how to retrieve the relevance values explicitly. Returned rows are
not ordered because the <code class="literal">SELECT</code> statement
includes neither <code class="literal">WHERE</code> nor <code class="literal">ORDER
BY</code> clauses:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT id, MATCH (title,body) AGAINST ('Tutorial')</code></strong>
-> <strong class="userinput"><code>FROM articles;</code></strong>
+----+-----------------------------------------+
| id | MATCH (title,body) AGAINST ('Tutorial') |
+----+-----------------------------------------+
| 1 | 0.65545833110809 |
| 2 | 0 |
| 3 | 0.66266459226608 |
| 4 | 0 |
| 5 | 0 |
| 6 | 0 |
+----+-----------------------------------------+
6 rows in set (0.00 sec)
</pre><p>
The following example is more complex. The query returns the
relevance values and it also sorts the rows in order of decreasing
relevance. To achieve this result, you should specify
<code class="literal">MATCH()</code> twice: once in the
<code class="literal">SELECT</code> list and once in the
<code class="literal">WHERE</code> clause. This causes no additional
overhead, because the MySQL optimizer notices that the two
<code class="literal">MATCH()</code> calls are identical and invokes the
full-text search code only once.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT id, body, MATCH (title,body) AGAINST</code></strong>
-> <strong class="userinput"><code>('Security implications of running MySQL as root') AS score</code></strong>
-> <strong class="userinput"><code>FROM articles WHERE MATCH (title,body) AGAINST</code></strong>
-> <strong class="userinput"><code>('Security implications of running MySQL as root');</code></strong>
+----+-------------------------------------+-----------------+
| id | body | score |
+----+-------------------------------------+-----------------+
| 4 | 1. Never run mysqld as root. 2. ... | 1.5219271183014 |
| 6 | When configured properly, MySQL ... | 1.3114095926285 |
+----+-------------------------------------+-----------------+
2 rows in set (0.00 sec)
</pre><p>
The MySQL <code class="literal">FULLTEXT</code> implementation regards any
sequence of true word characters (letters, digits, and
underscores) as a word. That sequence may also contain apostrophes
(‘<code class="literal">'</code>’), but not more than one in a
row. This means that <code class="literal">aaa'bbb</code> is regarded as one
word, but <code class="literal">aaa''bbb</code> is regarded as two words.
Apostrophes at the beginning or the end of a word are stripped by
the <code class="literal">FULLTEXT</code> parser;
<code class="literal">'aaa'bbb'</code> would be parsed as
<code class="literal">aaa'bbb</code>.
</p><p>
The <code class="literal">FULLTEXT</code> parser determines where words
start and end by looking for certain delimiter characters; for
example, ‘<code class="literal"> </code>’ (space),
‘<code class="literal">,</code>’ (comma), and
‘<code class="literal">.</code>’ (period). If words are not
separated by delimiters (as in, for example, Chinese), the
<code class="literal">FULLTEXT</code> parser cannot determine where a word
begins or ends. To be able to add words or other indexed terms in
such languages to a <code class="literal">FULLTEXT</code> index, you must
preprocess them so that they are separated by some arbitrary
delimiter such as ‘<code class="literal">"</code>’.
</p><p>
Some words are ignored in full-text searches:
</p><div class="itemizedlist"><ul type="disc"><li><p>
Any word that is too short is ignored. The default minimum
length of words that are found by full-text searches is four
characters.
</p></li><li><p>
Words in the stopword list are ignored. A stopword is a word
such as “<span class="quote">the</span>” or “<span class="quote">some</span>” that is so
common that it is considered to have zero semantic value.
There is a built-in stopword list, but it can be overwritten
by a user-defined list.
</p></li></ul></div><p>
The default stopword list is given in
<a href="mysqlqb_functions.html#fulltext-stopwords" title="Full-Text Stopwords">Full-Text Stopwords</a>. The default minimum word
length and stopword list can be changed as described in
<a href="mysqlqb_functions.html#fulltext-fine-tuning" title="Fine-Tuning MySQL Full-Text Search">Fine-Tuning MySQL Full-Text Search</a>.
</p><p>
Every correct word in the collection and in the query is weighted
according to its significance in the collection or query.
Consequently, a word that is present in many documents has a lower
weight (and may even have a zero weight), because it has lower
semantic value in this particular collection. Conversely, if the
word is rare, it receives a higher weight. The weights of the
words are combined to compute the relevance of the row.
</p><p>
Such a technique works best with large collections (in fact, it
was carefully tuned this way). For very small tables, word
distribution does not adequately reflect their semantic value, and
this model may sometimes produce bizarre results. For example,
although the word “<span class="quote">MySQL</span>” is present in every row of
the <code class="literal">articles</code> table shown earlier, a search for
the word produces no results:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT * FROM articles</code></strong>
-> <strong class="userinput"><code>WHERE MATCH (title,body) AGAINST ('MySQL');</code></strong>
Empty set (0.00 sec)
</pre><p>
The search result is empty because the word “<span class="quote">MySQL</span>”
is present in at least 50% of the rows. As such, it is effectively
treated as a stopword. For large datasets, this is the most
desirable behavior: A natural language query should not return
every second row from a 1GB table. For small datasets, it may be
less desirable.
</p><p>
A word that matches half of the rows in a table is less likely to
locate relevant documents. In fact, it most likely finds plenty of
irrelevant documents. We all know this happens far too often when
we are trying to find something on the Internet with a search
engine. It is with this reasoning that rows containing the word
are assigned a low semantic value for <span class="emphasis"><em>the particular
dataset in which they occur</em></span>. A given word may exceed
the 50% threshold in one dataset but not another.
</p><p>
The 50% threshold has a significant implication when you first try
full-text searching to see how it works: If you create a table and
insert only one or two rows of text into it, every word in the
text occurs in at least 50% of the rows. As a result, no search
returns any results. Be sure to insert at least three rows, and
preferably many more. Users who need to bypass the 50% limitation
can use the boolean search mode; see
<a href="mysqlqb_functions.html#fulltext-boolean" title="Boolean Full-Text Searches">Boolean Full-Text Searches</a>.
</p><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="fulltext-boolean"></a>Boolean Full-Text Searches</h3></div></div></div><p>
MySQL can perform boolean full-text searches using the
<code class="literal">IN BOOLEAN MODE</code> modifier:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT * FROM articles WHERE MATCH (title,body)</code></strong>
-> <strong class="userinput"><code>AGAINST ('+MySQL -YourSQL' IN BOOLEAN MODE);</code></strong>
+----+-----------------------+-------------------------------------+
| id | title | body |
+----+-----------------------+-------------------------------------+
| 1 | MySQL Tutorial | DBMS stands for DataBase ... |
| 2 | How To Use MySQL Well | After you went through a ... |
| 3 | Optimizing MySQL | In this tutorial we will show ... |
| 4 | 1001 MySQL Tricks | 1. Never run mysqld as root. 2. ... |
| 6 | MySQL Security | When configured properly, MySQL ... |
+----+-----------------------+-------------------------------------+
</pre><p>
The <code class="literal">+</code> and <code class="literal">-</code> operators
indicate that a word is required to be present or absent,
respectively, for a match to occur. Thus, this query retrieves
all the rows that contain the word “<span class="quote">MySQL</span>” but that
do <span class="emphasis"><em>not</em></span> contain the word
“<span class="quote">YourSQL</span>”.
</p><p>
Boolean full-text searches have these characteristics:
</p><div class="itemizedlist"><ul type="disc"><li><p>
They do not use the 50% threshold.
</p></li><li><p>
They do not automatically sort rows in order of decreasing
relevance. You can see this from the preceding query result:
The row with the highest relevance is the one that contains
“<span class="quote">MySQL</span>” twice, but it is listed last, not
first.
</p></li><li><p>
They can work even without a <code class="literal">FULLTEXT</code>
index, although a search executed in this fashion would be
quite slow.
</p></li><li><p>
The minimum and maximum word length full-text parameters
apply.
</p></li><li><p>
The stopword list applies.
</p></li></ul></div><p>
The boolean full-text search capability supports the following
operators:
</p><div class="itemizedlist"><ul type="disc"><li><p>
<code class="literal">+</code>
</p><p>
A leading plus sign indicates that this word
<span class="emphasis"><em>must</em></span> be present in each row that is
returned.
</p></li><li><p>
<code class="literal">-</code>
</p><p>
A leading minus sign indicates that this word must
<span class="emphasis"><em>not</em></span> be present in any of the rows that
are returned.
</p><p>
Note: The <code class="literal">-</code> operator acts only to exclude
rows that are otherwise matched by other search terms. Thus,
a boolean-mode search that contains only terms preceded by
<code class="literal">-</code> returns an empty result. It does not
return “<span class="quote">all rows except those containing any of the
excluded terms.</span>”
</p></li><li><p>
(no operator)
</p><p>
By default (when neither <code class="literal">+</code> nor
<code class="literal">-</code> is specified) the word is optional, but
the rows that contain it are rated higher. This mimics the
behavior of <code class="literal">MATCH() ... AGAINST()</code> without
the <code class="literal">IN BOOLEAN MODE</code> modifier.
</p></li><li><p>
<code class="literal">> <</code>
</p><p>
These two operators are used to change a word's contribution
to the relevance value that is assigned to a row. The
<code class="literal">></code> operator increases the contribution
and the <code class="literal"><</code> operator decreases it. See
the example following this list.
</p></li><li><p>
<code class="literal">( )</code>
</p><p>
Parentheses group words into subexpressions. Parenthesized
groups can be nested.
</p></li><li><p>
<code class="literal">~</code>
</p><p>
A leading tilde acts as a negation operator, causing the
word's contribution to the row's relevance to be negative.
This is useful for marking “<span class="quote">noise</span>” words. A row
containing such a word is rated lower than others, but is
not excluded altogether, as it would be with the
<code class="literal">-</code> operator.
</p></li><li><p>
<code class="literal">*</code>
</p><p>
The asterisk serves as the truncation (or wildcard)
operator. Unlike the other operators, it should be
<span class="emphasis"><em>appended</em></span> to the word to be affected.
Words match if they begin with the word preceding the
<code class="literal">*</code> operator.
</p><p>
If a stopword or too-short word is specified with the
truncation operator, it will not be stripped from a boolean
query. For example, a search for <code class="literal">'+word
+stopword*'</code> will likely return fewer rows than a
search for <code class="literal">'+word +stopword'</code> because the
former query remains as is and requires
<code class="literal">stopword*</code> to be present in a document.
The latter query is transformed to <code class="literal">+word</code>.
</p></li><li><p>
<code class="literal">"</code>
</p><p>
A phrase that is enclosed within double quote
(‘<code class="literal">"</code>’) characters matches only
rows that contain the phrase <span class="emphasis"><em>literally, as it was
typed</em></span>. The full-text engine splits the phrase
into words, performs a search in the
<code class="literal">FULLTEXT</code> index for the words. Prior to
MySQL 5.0.3, the engine then performed a substring search
for the phrase in the records that were found, so the match
must include non-word characters in the phrase. As of MySQL
5.0.3, non-word characters need not be matched exactly:
Phrase searching requires only that matches contain exactly
the same words as the phrase and in the same order. For
example, <code class="literal">"test phrase"</code> matches
<code class="literal">"test, phrase"</code> in MySQL 5.0.3, but not
before.
</p><p>
If the phrase contains no words that are in the index, the
result is empty. For example, if all words are either
stopwords or shorter than the minimum length of indexed
words, the result is empty.
</p></li></ul></div><p>
The following examples demonstrate some search strings that use
boolean full-text operators:
</p><div class="itemizedlist"><ul type="disc"><li><p>
<code class="literal">'apple banana'</code>
</p><p>
Find rows that contain at least one of the two words.
</p></li><li><p>
<code class="literal">'+apple +juice'</code>
</p><p>
Find rows that contain both words.
</p></li><li><p>
<code class="literal">'+apple macintosh'</code>
</p><p>
Find rows that contain the word “<span class="quote">apple</span>”, but
rank rows higher if they also contain
“<span class="quote">macintosh</span>”.
</p></li><li><p>
<code class="literal">'+apple -macintosh'</code>
</p><p>
Find rows that contain the word “<span class="quote">apple</span>” but not
“<span class="quote">macintosh</span>”.
</p></li><li><p>
<code class="literal">'+apple ~macintosh'</code>
</p><p>
Find rows that contain the word “<span class="quote">apple</span>”, but if
the row also contains the word “<span class="quote">macintosh</span>”,
rate it lower than if row does not. This is
“<span class="quote">softer</span>” than a search for <code class="literal">'+apple
-macintosh'</code>, for which the presence of
“<span class="quote">macintosh</span>” causes the row not to be returned
at all.
</p></li><li><p>
<code class="literal">'+apple +(>turnover <strudel)'</code>
</p><p>
Find rows that contain the words “<span class="quote">apple</span>” and
“<span class="quote">turnover</span>”, or “<span class="quote">apple</span>” and
“<span class="quote">strudel</span>” (in any order), but rank “<span class="quote">apple
turnover</span>” higher than “<span class="quote">apple strudel</span>”.
</p></li><li><p>
<code class="literal">'apple*'</code>
</p><p>
Find rows that contain words such as “<span class="quote">apple</span>”,
“<span class="quote">apples</span>”, “<span class="quote">applesauce</span>”, or
“<span class="quote">applet</span>”.
</p></li><li><p>
<code class="literal">'"some words"'</code>
</p><p>
Find rows that contain the exact phrase “<span class="quote">some
words</span>” (for example, rows that contain “<span class="quote">some
words of wisdom</span>” but not “<span class="quote">some noise
words</span>”). Note that the
‘<code class="literal">"</code>’ characters that enclose
the phrase are operator characters that delimit the phrase.
They are not the quotes that enclose the search string
itself.
</p></li></ul></div></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="fulltext-query-expansion"></a>Full-Text Searches with Query Expansion</h3></div></div></div><p>
Full-text search supports query expansion (and in particular,
its variant “<span class="quote">blind query expansion</span>”). This is
generally useful when a search phrase is too short, which often
means that the user is relying on implied knowledge that the
full-text search engine lacks. For example, a user searching for
“<span class="quote">database</span>” may really mean that
“<span class="quote">MySQL</span>”, “<span class="quote">Oracle</span>”, “<span class="quote">DB2</span>”,
and “<span class="quote">RDBMS</span>” all are phrases that should match
“<span class="quote">databases</span>” and should be returned, too. This is
implied knowledge.
</p><p>
Blind query expansion (also known as automatic relevance
feedback) is enabled by adding <code class="literal">WITH QUERY
EXPANSION</code> following the search phrase. It works by
performing the search twice, where the search phrase for the
second search is the original search phrase concatenated with
the few most highly relevant documents from the first search.
Thus, if one of these documents contains the word
“<span class="quote">databases</span>” and the word “<span class="quote">MySQL</span>”, the
second search finds the documents that contain the word
“<span class="quote">MySQL</span>” even if they do not contain the word
“<span class="quote">database</span>”. The following example shows this
difference:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT * FROM articles</code></strong>
-> <strong class="userinput"><code>WHERE MATCH (title,body) AGAINST ('database');</code></strong>
+----+-------------------+------------------------------------------+
| id | title | body |
+----+-------------------+------------------------------------------+
| 5 | MySQL vs. YourSQL | In the following database comparison ... |
| 1 | MySQL Tutorial | DBMS stands for DataBase ... |
+----+-------------------+------------------------------------------+
2 rows in set (0.00 sec)
mysql> <strong class="userinput"><code>SELECT * FROM articles</code></strong>
-> <strong class="userinput"><code>WHERE MATCH (title,body)</code></strong>
-> <strong class="userinput"><code>AGAINST ('database' WITH QUERY EXPANSION);</code></strong>
+----+-------------------+------------------------------------------+
| id | title | body |
+----+-------------------+------------------------------------------+
| 1 | MySQL Tutorial | DBMS stands for DataBase ... |
| 5 | MySQL vs. YourSQL | In the following database comparison ... |
| 3 | Optimizing MySQL | In this tutorial we will show ... |
+----+-------------------+------------------------------------------+
3 rows in set (0.00 sec)
</pre><p>
Another example could be searching for books by Georges Simenon
about Maigret, when a user is not sure how to spell
“<span class="quote">Maigret</span>”. A search for “<span class="quote">Megre and the
reluctant witnesses</span>” finds only “<span class="quote">Maigret and the
Reluctant Witnesses</span>” without query expansion. A search
with query expansion finds all books with the word
“<span class="quote">Maigret</span>” on the second pass.
</p><p>
<span class="bold"><strong>Note</strong></span>: Because blind query
expansion tends to increase noise significantly by returning
non-relevant documents, it is meaningful to use only when a
search phrase is rather short.
</p></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="fulltext-stopwords"></a>Full-Text Stopwords</h3></div></div></div><p>
The following table shows the default list of full-text
stopwords.
</p><div class="informaltable"><table border="1"><colgroup><col /><col /><col /><col /><col /></colgroup><tbody><tr><td>a's</td><td>able</td><td>about</td><td>above</td><td>according</td></tr><tr><td>accordingly</td><td>across</td><td>actually</td><td>after</td><td>afterwards</td></tr><tr><td>again</td><td>against</td><td>ain't</td><td>all</td><td>allow</td></tr><tr><td>allows</td><td>almost</td><td>alone</td><td>along</td><td>already</td></tr><tr><td>also</td><td>although</td><td>always</td><td>am</td><td>among</td></tr><tr><td>amongst</td><td>an</td><td>and</td><td>another</td><td>any</td></tr><tr><td>anybody</td><td>anyhow</td><td>anyone</td><td>anything</td><td>anyway</td></tr><tr><td>anyways</td><td>anywhere</td><td>apart</td><td>appear</td><td>appreciate</td></tr><tr><td>appropriate</td><td>are</td><td>aren't</td><td>around</td><td>as</td></tr><tr><td>aside</td><td>ask</td><td>asking</td><td>associated</td><td>at</td></tr><tr><td>available</td><td>away</td><td>awfully</td><td>be</td><td>became</td></tr><tr><td>because</td><td>become</td><td>becomes</td><td>becoming</td><td>been</td></tr><tr><td>before</td><td>beforehand</td><td>behind</td><td>being</td><td>believe</td></tr><tr><td>below</td><td>beside</td><td>besides</td><td>best</td><td>better</td></tr><tr><td>between</td><td>beyond</td><td>both</td><td>brief</td><td>but</td></tr><tr><td>by</td><td>c'mon</td><td>c's</td><td>came</td><td>can</td></tr><tr><td>can't</td><td>cannot</td><td>cant</td><td>cause</td><td>causes</td></tr><tr><td>certain</td><td>certainly</td><td>changes</td><td>clearly</td><td>co</td></tr><tr><td>com</td><td>come</td><td>comes</td><td>concerning</td><td>consequently</td></tr><tr><td>consider</td><td>considering</td><td>contain</td><td>containing</td><td>contains</td></tr><tr><td>corresponding</td><td>could</td><td>couldn't</td><td>course</td><td>currently</td></tr><tr><td>definitely</td><td>described</td><td>despite</td><td>did</td><td>didn't</td></tr><tr><td>different</td><td>do</td><td>does</td><td>doesn't</td><td>doing</td></tr><tr><td>don't</td><td>done</td><td>down</td><td>downwards</td><td>during</td></tr><tr><td>each</td><td>edu</td><td>eg</td><td>eight</td><td>either</td></tr><tr><td>else</td><td>elsewhere</td><td>enough</td><td>entirely</td><td>especially</td></tr><tr><td>et</td><td>etc</td><td>even</td><td>ever</td><td>every</td></tr><tr><td>everybody</td><td>everyone</td><td>everything</td><td>everywhere</td><td>ex</td></tr><tr><td>exactly</td><td>example</td><td>except</td><td>far</td><td>few</td></tr><tr><td>fifth</td><td>first</td><td>five</td><td>followed</td><td>following</td></tr><tr><td>follows</td><td>for</td><td>former</td><td>formerly</td><td>forth</td></tr><tr><td>four</td><td>from</td><td>further</td><td>furthermore</td><td>get</td></tr><tr><td>gets</td><td>getting</td><td>given</td><td>gives</td><td>go</td></tr><tr><td>goes</td><td>going</td><td>gone</td><td>got</td><td>gotten</td></tr><tr><td>greetings</td><td>had</td><td>hadn't</td><td>happens</td><td>hardly</td></tr><tr><td>has</td><td>hasn't</td><td>have</td><td>haven't</td><td>having</td></tr><tr><td>he</td><td>he's</td><td>hello</td><td>help</td><td>hence</td></tr><tr><td>her</td><td>here</td><td>here's</td><td>hereafter</td><td>hereby</td></tr><tr><td>herein</td><td>hereupon</td><td>hers</td><td>herself</td><td>hi</td></tr><tr><td>him</td><td>himself</td><td>his</td><td>hither</td><td>hopefully</td></tr><tr><td>how</td><td>howbeit</td><td>however</td><td>i'd</td><td>i'll</td></tr><tr><td>i'm</td><td>i've</td><td>ie</td><td>if</td><td>ignored</td></tr><tr><td>immediate</td><td>in</td><td>inasmuch</td><td>inc</td><td>indeed</td></tr><tr><td>indicate</td><td>indicated</td><td>indicates</td><td>inner</td><td>insofar</td></tr><tr><td>instead</td><td>into</td><td>inward</td><td>is</td><td>isn't</td></tr><tr><td>it</td><td>it'd</td><td>it'll</td><td>it's</td><td>its</td></tr><tr><td>itself</td><td>just</td><td>keep</td><td>keeps</td><td>kept</td></tr><tr><td>know</td><td>knows</td><td>known</td><td>last</td><td>lately</td></tr><tr><td>later</td><td>latter</td><td>latterly</td><td>least</td><td>less</td></tr><tr><td>lest</td><td>let</td><td>let's</td><td>like</td><td>liked</td></tr><tr><td>likely</td><td>little</td><td>look</td><td>looking</td><td>looks</td></tr><tr><td>ltd</td><td>mainly</td><td>many</td><td>may</td><td>maybe</td></tr><tr><td>me</td><td>mean</td><td>meanwhile</td><td>merely</td><td>might</td></tr><tr><td>more</td><td>moreover</td><td>most</td><td>mostly</td><td>much</td></tr><tr><td>must</td><td>my</td><td>myself</td><td>name</td><td>namely</td></tr><tr><td>nd</td><td>near</td><td>nearly</td><td>necessary</td><td>need</td></tr><tr><td>needs</td><td>neither</td><td>never</td><td>nevertheless</td><td>new</td></tr><tr><td>next</td><td>nine</td><td>no</td><td>nobody</td><td>non</td></tr><tr><td>none</td><td>noone</td><td>nor</td><td>normally</td><td>not</td></tr><tr><td>nothing</td><td>novel</td><td>now</td><td>nowhere</td><td>obviously</td></tr><tr><td>of</td><td>off</td><td>often</td><td>oh</td><td>ok</td></tr><tr><td>okay</td><td>old</td><td>on</td><td>once</td><td>one</td></tr><tr><td>ones</td><td>only</td><td>onto</td><td>or</td><td>other</td></tr><tr><td>others</td><td>otherwise</td><td>ought</td><td>our</td><td>ours</td></tr><tr><td>ourselves</td><td>out</td><td>outside</td><td>over</td><td>overall</td></tr><tr><td>own</td><td>particular</td><td>particularly</td><td>per</td><td>perhaps</td></tr><tr><td>placed</td><td>please</td><td>plus</td><td>possible</td><td>presumably</td></tr><tr><td>probably</td><td>provides</td><td>que</td><td>quite</td><td>qv</td></tr><tr><td>rather</td><td>rd</td><td>re</td><td>really</td><td>reasonably</td></tr><tr><td>regarding</td><td>regardless</td><td>regards</td><td>relatively</td><td>respectively</td></tr><tr><td>right</td><td>said</td><td>same</td><td>saw</td><td>say</td></tr><tr><td>saying</td><td>says</td><td>second</td><td>secondly</td><td>see</td></tr><tr><td>seeing</td><td>seem</td><td>seemed</td><td>seeming</td><td>seems</td></tr><tr><td>seen</td><td>self</td><td>selves</td><td>sensible</td><td>sent</td></tr><tr><td>serious</td><td>seriously</td><td>seven</td><td>several</td><td>shall</td></tr><tr><td>she</td><td>should</td><td>shouldn't</td><td>since</td><td>six</td></tr><tr><td>so</td><td>some</td><td>somebody</td><td>somehow</td><td>someone</td></tr><tr><td>something</td><td>sometime</td><td>sometimes</td><td>somewhat</td><td>somewhere</td></tr><tr><td>soon</td><td>sorry</td><td>specified</td><td>specify</td><td>specifying</td></tr><tr><td>still</td><td>sub</td><td>such</td><td>sup</td><td>sure</td></tr><tr><td>t's</td><td>take</td><td>taken</td><td>tell</td><td>tends</td></tr><tr><td>th</td><td>than</td><td>thank</td><td>thanks</td><td>thanx</td></tr><tr><td>that</td><td>that's</td><td>thats</td><td>the</td><td>their</td></tr><tr><td>theirs</td><td>them</td><td>themselves</td><td>then</td><td>thence</td></tr><tr><td>there</td><td>there's</td><td>thereafter</td><td>thereby</td><td>therefore</td></tr><tr><td>therein</td><td>theres</td><td>thereupon</td><td>these</td><td>they</td></tr><tr><td>they'd</td><td>they'll</td><td>they're</td><td>they've</td><td>think</td></tr><tr><td>third</td><td>this</td><td>thorough</td><td>thoroughly</td><td>those</td></tr><tr><td>though</td><td>three</td><td>through</td><td>throughout</td><td>thru</td></tr><tr><td>thus</td><td>to</td><td>together</td><td>too</td><td>took</td></tr><tr><td>toward</td><td>towards</td><td>tried</td><td>tries</td><td>truly</td></tr><tr><td>try</td><td>trying</td><td>twice</td><td>two</td><td>un</td></tr><tr><td>under</td><td>unfortunately</td><td>unless</td><td>unlikely</td><td>until</td></tr><tr><td>unto</td><td>up</td><td>upon</td><td>us</td><td>use</td></tr><tr><td>used</td><td>useful</td><td>uses</td><td>using</td><td>usually</td></tr><tr><td>value</td><td>various</td><td>very</td><td>via</td><td>viz</td></tr><tr><td>vs</td><td>want</td><td>wants</td><td>was</td><td>wasn't</td></tr><tr><td>way</td><td>we</td><td>we'd</td><td>we'll</td><td>we're</td></tr><tr><td>we've</td><td>welcome</td><td>well</td><td>went</td><td>were</td></tr><tr><td>weren't</td><td>what</td><td>what's</td><td>whatever</td><td>when</td></tr><tr><td>whence</td><td>whenever</td><td>where</td><td>where's</td><td>whereafter</td></tr><tr><td>whereas</td><td>whereby</td><td>wherein</td><td>whereupon</td><td>wherever</td></tr><tr><td>whether</td><td>which</td><td>while</td><td>whither</td><td>who</td></tr><tr><td>who's</td><td>whoever</td><td>whole</td><td>whom</td><td>whose</td></tr><tr><td>why</td><td>will</td><td>willing</td><td>wish</td><td>with</td></tr><tr><td>within</td><td>without</td><td>won't</td><td>wonder</td><td>would</td></tr><tr><td>would</td><td>wouldn't</td><td>yes</td><td>yet</td><td>you</td></tr><tr><td>you'd</td><td>you'll</td><td>you're</td><td>you've</td><td>your</td></tr><tr><td>yours</td><td>yourself</td><td>yourselves</td><td>zero</td><td> </td></tr></tbody></table></div></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="fulltext-restrictions"></a>Full-Text Restrictions</h3></div></div></div><div class="itemizedlist"><ul type="disc"><li><p>
Full-text searches are supported for
<code class="literal">MyISAM</code> tables only.
</p></li><li><p>
Full-text searches can be used with most multi-byte
character sets. The exception is that for Unicode, the
<code class="literal">utf8</code> character set can be used, but not
the <code class="literal">ucs2</code> character set.
</p></li><li><p>
Ideographic languages such as Chinese and Japanese do not
have word delimiters. Therefore, the
<code class="literal">FULLTEXT</code> parser <span class="emphasis"><em>cannot
determine where words begin and end in these and other such
languages</em></span>. The implications of this and some
workarounds for the problem are described in
<a href="mysqlqb_functions.html#fulltext-search" title="Full-Text Search Functions">Full-Text Search Functions</a>.
</p></li><li><p>
Although the use of multiple character sets within a single
table is supported, all columns in a
<code class="literal">FULLTEXT</code> index must use the same
character set and collation.
</p></li><li><p>
The <code class="literal">MATCH()</code> column list must match
exactly the column list in some <code class="literal">FULLTEXT</code>
index definition for the table, unless this
<code class="literal">MATCH()</code> is <code class="literal">IN BOOLEAN
MODE</code>. Boolean-mode searches can be done on
non-indexed columns, although they are likely to be slow.
</p></li><li><p>
The argument to <code class="literal">AGAINST()</code> must be a
constant string.
</p></li></ul></div></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="fulltext-fine-tuning"></a>Fine-Tuning MySQL Full-Text Search</h3></div></div></div><p>
MySQL's full-text search capability has few user-tunable
parameters. You can exert more control over full-text searching
behavior if you have a MySQL source distribution because some
changes require source code modifications. See
<a href="http://dev.mysql.com/doc/refman/5.0/en/installing-source.html" target="_top">MySQL Installation Using a Source Distribution</a>.
</p><p>
Note that full-text search is carefully tuned for the most
effectiveness. Modifying the default behavior in most cases can
actually decrease effectiveness. <span class="emphasis"><em>Do not alter the
MySQL sources unless you know what you are doing</em></span>.
</p><p>
Most full-text variables described in this section must be set
at server startup time. A server restart is required to change
them; they cannot be modified while the server is running.
</p><p>
Some variable changes require that you rebuild the
<code class="literal">FULLTEXT</code> indexes in your tables. Instructions
for doing this are given at the end of this section.
</p><div class="itemizedlist"><ul type="disc"><li><p>
The minimum and maximum lengths of words to be indexed are
defined by the <code class="literal">ft_min_word_len</code> and
<code class="literal">ft_max_word_len</code> system variables. (See
<a href="http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html" target="_top">System Variables</a>.) The default
minimum value is four characters; the default maximum is
version dependent. If you change either value, you must
rebuild your <code class="literal">FULLTEXT</code> indexes. For
example, if you want three-character words to be searchable,
you can set the <code class="literal">ft_min_word_len</code> variable
by putting the following lines in an option file:
</p><pre class="programlisting">[mysqld]
ft_min_word_len=3
</pre><p>
Then you must restart the server and rebuild your
<code class="literal">FULLTEXT</code> indexes. Note particularly the
remarks regarding <span><strong class="command">myisamchk</strong></span> in the
instructions following this list.
</p></li><li><p>
<a id="id2846051" class="indexterm"></a>
<a id="id2846062" class="indexterm"></a>
To override the default stopword list, set the
<code class="literal">ft_stopword_file</code> system variable. (See
<a href="http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html" target="_top">System Variables</a>.) The variable
value should be the pathname of the file containing the
stopword list, or the empty string to disable stopword
filtering. After changing the value of this variable or the
contents of the stopword file, restart the server and
rebuild your <code class="literal">FULLTEXT</code> indexes.
</p><p>
The stopword list is free-form. That is, you may use any
non-alphanumeric character such as newline, space, or comma
to separate stopwords. Exceptions are the underscore
character (‘<code class="literal">_</code>’) and a single
apostrophe (‘<code class="literal">'</code>’) which are
treated as part of a word. The character set of the stopword
list is the server's default character set; see
<a href="http://dev.mysql.com/doc/refman/5.0/en/charset-server.html" target="_top">Server Character Set and Collation</a>.
</p></li><li><p>
The 50% threshold for natural language searches is
determined by the particular weighting scheme chosen. To
disable it, look for the following line in
<code class="filename">myisam/ftdefs.h</code>:
</p><pre class="programlisting">#define GWS_IN_USE GWS_PROB
</pre><p>
Change that line to this:
</p><pre class="programlisting">#define GWS_IN_USE GWS_FREQ
</pre><p>
Then recompile MySQL. There is no need to rebuild the
indexes in this case. <span class="bold"><strong>Note</strong></span>:
By making this change, you <span class="emphasis"><em>severely</em></span>
decrease MySQL's ability to provide adequate relevance
values for the <code class="literal">MATCH()</code> function. If you
really need to search for such common words, it would be
better to search using <code class="literal">IN BOOLEAN MODE</code>
instead, which does not observe the 50% threshold.
</p></li><li><p>
To change the operators used for boolean full-text searches,
set the <code class="literal">ft_boolean_syntax</code> system
variable. This variable can be changed while the server is
running, but you must have the <code class="literal">SUPER</code>
privilege to do so. No rebuilding of indexes is necessary in
this case. See <a href="http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html" target="_top">System Variables</a>,
which describes the rules governing how to set this
variable.
</p></li><li><p>
If you want to change the set of characters that are
considered word characters, you can do so in two ways.
Suppose that you want to treat the hyphen character ('-') as
a word character. Use either of these methods:
</p><div class="itemizedlist"><ul type="circle"><li><p>
Modify the MySQL source: In
<code class="filename">myisam/ftdefs.h</code>, see the
<code class="literal">true_word_char()</code> and
<code class="literal">misc_word_char()</code> macros. Add
<code class="literal">'-'</code> to one of those macros and
recompile MySQL.
</p></li><li><p>
Modify a character set file: This requires no
recompilation. The <code class="literal">true_word_char()</code>
macro uses a “<span class="quote">character type</span>” table to
distinguish letters and numbers from other characters. .
You can edit the
<code class="literal"><ctype><map></code> contents in
one of the character set XML files to specify that
<code class="literal">'-'</code> is a “<span class="quote">letter.</span>” Then
use the given character set for your
<code class="literal">FULLTEXT</code> indexes.
</p></li></ul></div><p>
After making the modification, you must rebuild the indexes
for each table that contains any <code class="literal">FULLTEXT</code>
indexes.
</p></li></ul></div><p>
If you modify full-text variables that affect indexing
(<code class="literal">ft_min_word_len</code>,
<code class="literal">ft_max_word_len</code>, or
<code class="literal">ft_stopword_file</code>), or if you change the
stopword file itself, you must rebuild your
<code class="literal">FULLTEXT</code> indexes after making the changes and
restarting the server. To rebuild the indexes in this case, it
is sufficient to do a <code class="literal">QUICK</code> repair operation:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>REPAIR TABLE <em class="replaceable"><code>tbl_name</code></em> QUICK;</code></strong>
</pre><p>
Each table that contains any <code class="literal">FULLTEXT</code> index
must be repaired as just shown. Otherwise, queries for the table
may yield incorrect results, and modifications to the table will
cause the server to see the table as corrupt and in need of
repair.
</p><p>
Note that if you use <span><strong class="command">myisamchk</strong></span> to perform an
operation that modifies table indexes (such as repair or
analyze), the <code class="literal">FULLTEXT</code> indexes are rebuilt
using the <span class="emphasis"><em>default</em></span> full-text parameter
values for minimum word length, maximum word length, and
stopword file unless you specify otherwise. This can result in
queries failing.
</p><p>
The problem occurs because these parameters are known only by
the server. They are not stored in <code class="literal">MyISAM</code>
index files. To avoid the problem if you have modified the
minimum or maximum word length or stopword file values used by
the server, specify the same <code class="literal">ft_min_word_len</code>,
<code class="literal">ft_max_word_len</code>, and
<code class="literal">ft_stopword_file</code> values to
<span><strong class="command">myisamchk</strong></span> that you use for
<span><strong class="command">mysqld</strong></span>. For example, if you have set the
minimum word length to 3, you can repair a table with
<span><strong class="command">myisamchk</strong></span> like this:
</p><pre class="programlisting">shell> <strong class="userinput"><code>myisamchk --recover --ft_min_word_len=3 <em class="replaceable"><code>tbl_name</code></em>.MYI</code></strong>
</pre><p>
To ensure that <span><strong class="command">myisamchk</strong></span> and the server use
the same values for full-text parameters, place each one in both
the <code class="literal">[mysqld]</code> and
<code class="literal">[myisamchk]</code> sections of an option file:
</p><pre class="programlisting">[mysqld]
ft_min_word_len=3
[myisamchk]
ft_min_word_len=3
</pre><p>
An alternative to using <span><strong class="command">myisamchk</strong></span> is to use
the <code class="literal">REPAIR TABLE</code>, <code class="literal">ANALYZE
TABLE</code>, <code class="literal">OPTIMIZE TABLE</code>, or
<code class="literal">ALTER TABLE</code> statements. These statements are
performed by the server, which knows the proper full-text
parameter values to use.
</p></div></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="cast-functions"></a>Cast Functions and Operators</h2></div></div></div><a id="id2846471" class="indexterm"></a><a id="id2846478" class="indexterm"></a><a id="id2846485" class="indexterm"></a><a id="id2846495" class="indexterm"></a><a id="id2846502" class="indexterm"></a><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><thead><tr><th><span class="bold"><strong>Name</strong></span></th><th><span class="bold"><strong>Description</strong></span></th></tr></thead><tbody><tr><td><a href="mysqlqb_functions.html#operator_binary"><code class="literal">BINARY</code></a></td><td>Cast a string to a binary string</td></tr><tr><td><a href="mysqlqb_functions.html#function_cast"><code class="literal">CAST()</code></a></td><td>Cast a value as a certain type</td></tr></tbody></table></div><div class="itemizedlist"><ul type="disc"><li><p><a id="operator_binary"></a>
<a id="id2846593" class="indexterm"></a>
<code class="literal">BINARY</code>
</p><p>
The <code class="literal">BINARY</code> operator casts the string
following it to a binary string. This is an easy way to force
a column comparison to be done byte by byte rather than
character by character. This causes the comparison to be case
sensitive even if the column isn't defined as
<code class="literal">BINARY</code> or <code class="literal">BLOB</code>.
<code class="literal">BINARY</code> also causes trailing spaces to be
significant.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 'a' = 'A';</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT BINARY 'a' = 'A';</code></strong>
-> 0
mysql> <strong class="userinput"><code>SELECT 'a' = 'a ';</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT BINARY 'a' = 'a ';</code></strong>
-> 0
</pre><p>
In a comparison, <code class="literal">BINARY</code> affects the entire
operation; it can be given before either operand with the same
result.
</p><p>
<code class="literal">BINARY <em class="replaceable"><code>str</code></em></code> is
shorthand for <code class="literal">CAST(<em class="replaceable"><code>str</code></em> AS
BINARY)</code>.
</p><p>
Note that in some contexts, if you cast an indexed column to
<code class="literal">BINARY</code>, MySQL is not able to use the index
efficiently.
</p></li><li><p><a id="function_cast"></a>
<a id="id2846730" class="indexterm"></a>
<a id="id2846736" class="indexterm"></a>
<code class="literal">CAST(<em class="replaceable"><code>expr</code></em> AS
<em class="replaceable"><code>type</code></em>)</code>,
<code class="literal">CONVERT(<em class="replaceable"><code>expr</code></em>,<em class="replaceable"><code>type</code></em>)</code>,
<code class="literal">CONVERT(<em class="replaceable"><code>expr</code></em> USING
<em class="replaceable"><code>transcoding_name</code></em>)</code>
</p><p>
The <code class="literal">CAST()</code> and <code class="literal">CONVERT()</code>
functions take a value of one type and produce a value of
another type.
</p><p>
The <em class="replaceable"><code>type</code></em> can be one of the
following values:
</p><div class="itemizedlist"><ul type="circle"><li><p>
<code class="literal">BINARY[(<em class="replaceable"><code>N</code></em>)]</code>
</p></li><li><p>
<code class="literal">CHAR[(<em class="replaceable"><code>N</code></em>)]</code>
</p></li><li><p>
<code class="literal">DATE</code>
</p></li><li><p>
<code class="literal">DATETIME</code>
</p></li><li><p>
<code class="literal">DECIMAL</code>
</p></li><li><p>
<code class="literal">SIGNED [INTEGER]</code>
</p></li><li><p>
<code class="literal">TIME</code>
</p></li><li><p>
<code class="literal">UNSIGNED [INTEGER]</code>
</p></li></ul></div><p>
<code class="literal">BINARY</code> produces a string with the
<code class="literal">BINARY</code> data type. See
<a href="http://dev.mysql.com/doc/refman/5.0/en/binary-varbinary.html" target="_top">The <code class="literal">BINARY</code> and <code class="literal">VARBINARY</code> Types</a> for a description of how
this affects comparisons. If the optional length
<em class="replaceable"><code>N</code></em> is given,
<code class="literal">BINARY(<em class="replaceable"><code>N</code></em>)</code> causes
the cast to use no more than <em class="replaceable"><code>N</code></em>
bytes of the argument. As of MySQL 5.0.17, values shorter than
<em class="replaceable"><code>N</code></em> bytes are padded with
<code class="literal">0x00</code> bytes to a length of
<em class="replaceable"><code>N</code></em>.
</p><p>
<code class="literal">CHAR(<em class="replaceable"><code>N</code></em>)</code> causes
the cast to use no more than <em class="replaceable"><code>N</code></em>
characters of the argument.
</p><p>
The <code class="literal">DECIMAL</code> type is available as of MySQL
5.0.8.
</p><p>
<code class="literal">CAST()</code> and <code class="literal">CONVERT(... USING
...)</code> are standard SQL syntax. The
non-<code class="literal">USING</code> form of
<code class="literal">CONVERT()</code> is ODBC syntax.
</p><p>
<code class="literal">CONVERT()</code> with <code class="literal">USING</code> is
used to convert data between different character sets. In
MySQL, transcoding names are the same as the corresponding
character set names. For example, this statement converts the
string <code class="literal">'abc'</code> in the default character set
to the corresponding string in the <code class="literal">utf8</code>
character set:
</p><pre class="programlisting">SELECT CONVERT('abc' USING utf8);
</pre></li></ul></div><p>
Normally, you cannot compare a <code class="literal">BLOB</code> value or
other binary string in case-insensitive fashion because binary
strings have no character set, and thus no concept of lettercase.
To perform a case-insensitive comparison, use the
<code class="literal">CONVERT()</code> function to convert the value to a
non-binary string. If the character set of the result has a
case-insensitive collation, the <code class="literal">LIKE</code> operation
is not case sensitive:
</p><pre class="programlisting">SELECT 'A' LIKE CONVERT(<em class="replaceable"><code>blob_col</code></em> USING latin1) FROM <em class="replaceable"><code>tbl_name</code></em>;
</pre><p>
To use a different character set, substitute its name for
<code class="literal">latin1</code> in the preceding statement. To ensure
that a case-insensitive collation is used, specify a
<code class="literal">COLLATE</code> clause following the
<code class="literal">CONVERT()</code> call.
</p><p>
<code class="literal">CONVERT()</code> can be used more generally for
comparing strings that are represented in different character
sets.
</p><p>
The cast functions are useful when you want to create a column
with a specific type in a <code class="literal">CREATE ... SELECT</code>
statement:
</p><pre class="programlisting">CREATE TABLE new_table SELECT CAST('2000-01-01' AS DATE);
</pre><p>
The functions also can be useful for sorting
<code class="literal">ENUM</code> columns in lexical order. Normally,
sorting of <code class="literal">ENUM</code> columns occurs using the
internal numeric values. Casting the values to
<code class="literal">CHAR</code> results in a lexical sort:
</p><pre class="programlisting">SELECT <em class="replaceable"><code>enum_col</code></em> FROM <em class="replaceable"><code>tbl_name</code></em> ORDER BY CAST(<em class="replaceable"><code>enum_col</code></em> AS CHAR);
</pre><p>
<code class="literal">CAST(<em class="replaceable"><code>str</code></em> AS BINARY)</code>
is the same thing as <code class="literal">BINARY
<em class="replaceable"><code>str</code></em></code>.
<code class="literal">CAST(<em class="replaceable"><code>expr</code></em> AS CHAR)</code>
treats the expression as a string with the default character set.
</p><p>
<code class="literal">CAST()</code> also changes the result if you use it as
part of a more complex expression such as <code class="literal">CONCAT('Date:
',CAST(NOW() AS DATE))</code>.
</p><p>
You should not use <code class="literal">CAST()</code> to extract data in
different formats but instead use string functions like
<code class="literal">LEFT()</code> or <code class="literal">EXTRACT()</code>. See
<a href="mysqlqb_functions.html#date-and-time-functions" title="Date and Time Functions">Date and Time Functions</a>.
</p><p>
To cast a string to a numeric value in numeric context, you
normally do not have to do anything other than to use the string
value as though it were a number:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 1+'1';</code></strong>
-> 2
</pre><p>
If you use a number in string context, the number automatically is
converted to a <code class="literal">BINARY</code> string.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT CONCAT('hello you ',2);</code></strong>
-> 'hello you 2'
</pre><p>
MySQL supports arithmetic with both signed and unsigned 64-bit
values. If you are using numeric operators (such as
<code class="literal">+</code> or <code class="literal">-</code>) and one of the
operands is an unsigned integer, the result is unsigned. You can
override this by using the <code class="literal">SIGNED</code> and
<code class="literal">UNSIGNED</code> cast operators to cast the operation
to a signed or unsigned 64-bit integer, respectively.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT CAST(1-2 AS UNSIGNED)</code></strong>
-> 18446744073709551615
mysql> <strong class="userinput"><code>SELECT CAST(CAST(1-2 AS UNSIGNED) AS SIGNED);</code></strong>
-> -1
</pre><p>
Note that if either operand is a floating-point value, the result
is a floating-point value and is not affected by the preceding
rule. (In this context, <code class="literal">DECIMAL</code> column values
are regarded as floating-point values.)
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT CAST(1 AS UNSIGNED) - 2.0;</code></strong>
-> -1.0
</pre><p>
If you are using a string in an arithmetic operation, this is
converted to a floating-point number.
</p><p>
If you convert a “<span class="quote">zero</span>” date string to a date,
<code class="literal">CONVERT()</code> and <code class="literal">CAST()</code> return
<code class="literal">NULL</code> when the <code class="literal">NO_ZERO_DATE</code>
SQL mode is enabled. As of MySQL 5.0.4, they also produce a
warning.
</p></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="other-functions"></a>Other Functions</h2></div></div></div><div class="toc"><dl><dt><span class="section"><a href="mysqlqb_functions.html#bit-functions">Bit Functions</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#encryption-functions">Encryption and Compression Functions</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#information-functions">Information Functions</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#miscellaneous-functions">Miscellaneous Functions</a></span></dt></dl></div><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><thead><tr><th><span class="bold"><strong>Name</strong></span></th><th><span class="bold"><strong>Description</strong></span></th></tr></thead><tbody><tr><td><a href="mysqlqb_functions.html#function_aes-encrypt"><code class="literal">AES_DECRYPT()</code></a></td><td>Decrypt using AES</td></tr><tr><td><a href="mysqlqb_functions.html#function_aes-encrypt"><code class="literal">AES_ENCRYPT()</code></a></td><td>Encrypt using AES</td></tr><tr><td><a href="mysqlqb_functions.html#function_benchmark"><code class="literal">BENCHMARK()</code></a></td><td>Repeatedly execute an expression</td></tr><tr><td><a href="mysqlqb_functions.html#function_bit-and"><code class="literal">BIT_AND()</code></a></td><td>Return bitwise and</td></tr><tr><td><a href="mysqlqb_functions.html#function_bit-count"><code class="literal">BIT_COUNT()</code></a></td><td>Return the number of bits that are set</td></tr><tr><td><a href="mysqlqb_functions.html#function_bit-or"><code class="literal">BIT_OR()</code></a></td><td>Return bitwise or</td></tr><tr><td><a href="mysqlqb_functions.html#function_bit-xor"><code class="literal">BIT_XOR()</code></a></td><td>Return bitwise xor</td></tr><tr><td><a href="mysqlqb_functions.html#operator_bitwise-and"><code class="literal">&</code></a></td><td>Bitwise AND</td></tr><tr><td><a href="mysqlqb_functions.html#operator_bitwise-or"><code class="literal">|</code></a></td><td>Bitwise OR</td></tr><tr><td><a href="mysqlqb_functions.html#operator_bitwise-xor"><code class="literal">^</code></a></td><td>Bitwise XOR</td></tr><tr><td><a href="mysqlqb_functions.html#function_charset"><code class="literal">CHARSET()</code></a></td><td>Return the character set of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_coercibility"><code class="literal">COERCIBILITY()</code></a></td><td>Return the collation coercibility value of the string argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_collation"><code class="literal">COLLATION()</code></a></td><td>Return the collation of the string argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_compress"><code class="literal">COMPRESS()</code></a></td><td>Return result as a binary string</td></tr><tr><td><a href="mysqlqb_functions.html#function_connection-id"><code class="literal">CONNECTION_ID()</code></a></td><td>Return the connection ID (thread ID) for the connection</td></tr><tr><td><a href="mysqlqb_functions.html#function_count"><code class="literal">COUNT()</code></a></td><td>Return a count of the number of rows returned</td></tr><tr><td><a href="mysqlqb_functions.html#function_current-user"><code class="literal">CURRENT_USER()</code>, <code class="literal">CURRENT_USER</code></a></td><td>Return the username and hostname combination</td></tr><tr><td><a href="mysqlqb_functions.html#function_database"><code class="literal">DATABASE()</code></a></td><td>Return the default (current) database name</td></tr><tr><td><a href="mysqlqb_functions.html#function_decode"><code class="literal">DECODE()</code></a></td><td>Decodes a string encrypted using ENCODE()</td></tr><tr><td><a href="mysqlqb_functions.html#function_default"><code class="literal">DEFAULT()</code></a></td><td>Return the default value for a table column</td></tr><tr><td><a href="mysqlqb_functions.html#function_des-decrypt"><code class="literal">DES_DECRYPT()</code></a></td><td>Decrypt a string</td></tr><tr><td><a href="mysqlqb_functions.html#function_des-encrypt"><code class="literal">DES_ENCRYPT()</code></a></td><td>Decrypt a string</td></tr><tr><td><a href="mysqlqb_functions.html#function_encode"><code class="literal">ENCODE()</code></a></td><td>Encode a string</td></tr><tr><td><a href="mysqlqb_functions.html#function_encrypt"><code class="literal">ENCRYPT()</code></a></td><td>Encrypt a string</td></tr><tr><td><a href="mysqlqb_functions.html#function_format"><code class="literal">FORMAT()</code></a></td><td>Return a number formatted to specified number of decimal places</td></tr><tr><td><a href="mysqlqb_functions.html#function_found-rows"><code class="literal">FOUND_ROWS()</code></a></td><td>For a SELECT with a LIMIT clause, the number of rows that would be returned were there no LIMIT clause</td></tr><tr><td><a href="mysqlqb_functions.html#function_get-lock"><code class="literal">GET_LOCK()</code></a></td><td>Get a named lock</td></tr><tr><td><a href="mysqlqb_functions.html#function_inet-aton"><code class="literal">INET_ATON()</code></a></td><td>Return the numeric value of an IP address</td></tr><tr><td><a href="mysqlqb_functions.html#function_inet-ntoa"><code class="literal">INET_NTOA()</code></a></td><td>Return the IP address from a numeric value</td></tr><tr><td><a href="mysqlqb_functions.html#function_is-free-lock"><code class="literal">IS_FREE_LOCK()</code></a></td><td>Checks whether the named lock is free</td></tr><tr><td><a href="mysqlqb_functions.html#function_is-used-lock"><code class="literal">IS_USED_LOCK()</code></a></td><td>Checks whether the named lock is in use. Return connection identifier if true.</td></tr><tr><td><a href="mysqlqb_functions.html#function_last-insert-id"><code class="literal">LAST_INSERT_ID()</code></a></td><td>Value of the AUTOINCREMENT column for the last INSERT</td></tr><tr><td><a href="mysqlqb_functions.html#operator_left-shift"><code class="literal"><<</code></a></td><td>Left shift</td></tr><tr><td><a href="mysqlqb_functions.html#function_master-pos-wait"><code class="literal">MASTER_POS_WAIT()</code></a></td><td>Block until the slave has read and applied all updates up to the specified position</td></tr><tr><td><a href="mysqlqb_functions.html#function_md5"><code class="literal">MD5()</code></a></td><td>Calculate MD5 checksum</td></tr><tr><td><a href="mysqlqb_functions.html#function_name-const"><code class="literal">NAME_CONST()</code></a>(v5.0.12)</td><td>Causes the column to have the given name</td></tr><tr><td><a href="mysqlqb_functions.html#function_old-password"><code class="literal">OLD_PASSWORD()</code></a></td><td>Return the value of the old (pre-4.1) implementation of PASSWORD</td></tr><tr><td><a href="mysqlqb_functions.html#function_password"><code class="literal">PASSWORD()</code></a></td><td>Calculate and return a password string</td></tr><tr><td><a href="mysqlqb_functions.html#function_release-lock"><code class="literal">RELEASE_LOCK()</code></a></td><td>Releases the named lock</td></tr><tr><td><a href="mysqlqb_functions.html#operator_right-shift"><code class="literal">>></code></a></td><td>Right shift</td></tr><tr><td><a href="mysqlqb_functions.html#function_row-count"><code class="literal">ROW_COUNT()</code></a></td><td>The number of rows updated</td></tr><tr><td><a href="mysqlqb_functions.html#function_schema"><code class="literal">SCHEMA()</code></a></td><td>A synonym for DATABASE()</td></tr><tr><td><a href="mysqlqb_functions.html#function_session-user"><code class="literal">SESSION_USER()</code></a></td><td>Synonym for USER()</td></tr><tr><td><a href="mysqlqb_functions.html#function_sha1"><code class="literal">SHA1()</code>, <code class="literal">SHA()</code></a></td><td>Calculate an SHA-1 160-bit checksum</td></tr><tr><td><a href="mysqlqb_functions.html#function_sleep"><code class="literal">SLEEP()</code></a></td><td>Sleep for a number of seconds</td></tr><tr><td><a href="mysqlqb_functions.html#function_system-user"><code class="literal">SYSTEM_USER()</code></a></td><td>Synonym for USER()</td></tr><tr><td><a href="mysqlqb_functions.html#operator_tilde"><code class="literal">~</code></a></td><td>Invert bits</td></tr><tr><td><a href="mysqlqb_functions.html#function_uncompress"><code class="literal">UNCOMPRESS()</code></a></td><td>Uncompress a string compressed</td></tr><tr><td><a href="mysqlqb_functions.html#function_uncompressed-length"><code class="literal">UNCOMPRESSED_LENGTH()</code></a></td><td>Return the length of a string before compression</td></tr><tr><td><a href="mysqlqb_functions.html#function_user"><code class="literal">USER()</code></a></td><td>Return the current username and hostname</td></tr><tr><td><a href="mysqlqb_functions.html#function_uuid"><code class="literal">UUID()</code></a></td><td>Return a Universal Unique Identifier (UUID)</td></tr><tr><td><a href="mysqlqb_functions.html#function_values"><code class="literal">VALUES()</code></a></td><td>Defines the values to be used during an INSERT</td></tr></tbody></table></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="bit-functions"></a>Bit Functions</h3></div></div></div><a id="id2847948" class="indexterm"></a><a id="id2847955" class="indexterm"></a><a id="id2847966" class="indexterm"></a><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><thead><tr><th><span class="bold"><strong>Name</strong></span></th><th><span class="bold"><strong>Description</strong></span></th></tr></thead><tbody><tr><td><a href="mysqlqb_functions.html#function_bit-and"><code class="literal">BIT_AND()</code></a></td><td>Return bitwise and</td></tr><tr><td><a href="mysqlqb_functions.html#function_bit-count"><code class="literal">BIT_COUNT()</code></a></td><td>Return the number of bits that are set</td></tr><tr><td><a href="mysqlqb_functions.html#function_bit-or"><code class="literal">BIT_OR()</code></a></td><td>Return bitwise or</td></tr><tr><td><a href="mysqlqb_functions.html#function_bit-xor"><code class="literal">BIT_XOR()</code></a></td><td>Return bitwise xor</td></tr><tr><td><a href="mysqlqb_functions.html#operator_bitwise-and"><code class="literal">&</code></a></td><td>Bitwise AND</td></tr><tr><td><a href="mysqlqb_functions.html#operator_bitwise-or"><code class="literal">|</code></a></td><td>Bitwise OR</td></tr><tr><td><a href="mysqlqb_functions.html#operator_bitwise-xor"><code class="literal">^</code></a></td><td>Bitwise XOR</td></tr><tr><td><a href="mysqlqb_functions.html#operator_left-shift"><code class="literal"><<</code></a></td><td>Left shift</td></tr><tr><td><a href="mysqlqb_functions.html#operator_right-shift"><code class="literal">>></code></a></td><td>Right shift</td></tr><tr><td><a href="mysqlqb_functions.html#operator_tilde"><code class="literal">~</code></a></td><td>Invert bits</td></tr></tbody></table></div><p>
MySQL uses <code class="literal">BIGINT</code> (64-bit) arithmetic for bit
operations, so these operators have a maximum range of 64 bits.
</p><div class="itemizedlist"><ul type="disc"><li><p><a id="operator_bitwise-or"></a>
<a id="id2848156" class="indexterm"></a>
<a id="id2848162" class="indexterm"></a>
<code class="literal">|</code>
</p><p>
Bitwise OR:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 29 | 15;</code></strong>
-> 31
</pre><p>
The result is an unsigned 64-bit integer.
</p></li><li><p><a id="operator_bitwise-and"></a>
<a id="id2848230" class="indexterm"></a>
<a id="id2848237" class="indexterm"></a>
<code class="literal">&</code>
</p><p>
Bitwise AND:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 29 & 15;</code></strong>
-> 13
</pre><p>
The result is an unsigned 64-bit integer.
</p></li><li><p><a id="operator_bitwise-xor"></a>
<a id="id2848305" class="indexterm"></a>
<a id="id2848312" class="indexterm"></a>
<code class="literal">^</code>
</p><p>
Bitwise XOR:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 1 ^ 1;</code></strong>
-> 0
mysql> <strong class="userinput"><code>SELECT 1 ^ 0;</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT 11 ^ 3;</code></strong>
-> 8
</pre><p>
The result is an unsigned 64-bit integer.
</p></li><li><p><a id="operator_left-shift"></a>
<a id="id2848388" class="indexterm"></a>
<code class="literal"><<</code>
</p><p>
Shifts a longlong (<code class="literal">BIGINT</code>) number to the
left.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 1 << 2;</code></strong>
-> 4
</pre><p>
The result is an unsigned 64-bit integer.
</p></li><li><p><a id="operator_right-shift"></a>
<a id="id2848458" class="indexterm"></a>
<code class="literal">>></code>
</p><p>
Shifts a longlong (<code class="literal">BIGINT</code>) number to the
right.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 4 >> 2;</code></strong>
-> 1
</pre><p>
The result is an unsigned 64-bit integer.
</p></li><li><p><a id="operator_tilde"></a>
<a id="id2848527" class="indexterm"></a>
<code class="literal">~</code>
</p><p>
Invert all bits.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT 5 & ~1;</code></strong>
-> 4
</pre><p>
The result is an unsigned 64-bit integer.
</p></li><li><p><a id="function_bit-count"></a>
<a id="id2848591" class="indexterm"></a>
<code class="literal">BIT_COUNT(<em class="replaceable"><code>N</code></em>)</code>
</p><p>
Returns the number of bits that are set in the argument
<em class="replaceable"><code>N</code></em>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT BIT_COUNT(29), BIT_COUNT(b'101010');</code></strong>
-> 4, 3
</pre></li></ul></div></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="encryption-functions"></a>Encryption and Compression Functions</h3></div></div></div><a id="id2848652" class="indexterm"></a><a id="id2848659" class="indexterm"></a><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><thead><tr><th><span class="bold"><strong>Name</strong></span></th><th><span class="bold"><strong>Description</strong></span></th></tr></thead><tbody><tr><td><a href="mysqlqb_functions.html#function_aes-encrypt"><code class="literal">AES_DECRYPT()</code></a></td><td>Decrypt using AES</td></tr><tr><td><a href="mysqlqb_functions.html#function_aes-encrypt"><code class="literal">AES_ENCRYPT()</code></a></td><td>Encrypt using AES</td></tr><tr><td><a href="mysqlqb_functions.html#function_compress"><code class="literal">COMPRESS()</code></a></td><td>Return result as a binary string</td></tr><tr><td><a href="mysqlqb_functions.html#function_decode"><code class="literal">DECODE()</code></a></td><td>Decodes a string encrypted using ENCODE()</td></tr><tr><td><a href="mysqlqb_functions.html#function_des-decrypt"><code class="literal">DES_DECRYPT()</code></a></td><td>Decrypt a string</td></tr><tr><td><a href="mysqlqb_functions.html#function_des-encrypt"><code class="literal">DES_ENCRYPT()</code></a></td><td>Decrypt a string</td></tr><tr><td><a href="mysqlqb_functions.html#function_encode"><code class="literal">ENCODE()</code></a></td><td>Encode a string</td></tr><tr><td><a href="mysqlqb_functions.html#function_encrypt"><code class="literal">ENCRYPT()</code></a></td><td>Encrypt a string</td></tr><tr><td><a href="mysqlqb_functions.html#function_md5"><code class="literal">MD5()</code></a></td><td>Calculate MD5 checksum</td></tr><tr><td><a href="mysqlqb_functions.html#function_old-password"><code class="literal">OLD_PASSWORD()</code></a></td><td>Return the value of the old (pre-4.1) implementation of PASSWORD</td></tr><tr><td><a href="mysqlqb_functions.html#function_password"><code class="literal">PASSWORD()</code></a></td><td>Calculate and return a password string</td></tr><tr><td><a href="mysqlqb_functions.html#function_sha1"><code class="literal">SHA1()</code>, <code class="literal">SHA()</code></a></td><td>Calculate an SHA-1 160-bit checksum</td></tr><tr><td><a href="mysqlqb_functions.html#function_uncompress"><code class="literal">UNCOMPRESS()</code></a></td><td>Uncompress a string compressed</td></tr><tr><td><a href="mysqlqb_functions.html#function_uncompressed-length"><code class="literal">UNCOMPRESSED_LENGTH()</code></a></td><td>Return the length of a string before compression</td></tr></tbody></table></div><p>
The functions in this section perform encryption and decryption,
and compression and uncompression:
</p><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><tbody><tr><td><span class="bold"><strong>Compression or encryption</strong></span></td><td><span class="bold"><strong>Uncompression or decryption</strong></span></td></tr><tr><td><a href="mysqlqb_functions.html#function_aes-encrypt">AES_ENCRYT()</a></td><td><a href="mysqlqb_functions.html#function_aes-encrypt">AES_DECRYPT()</a></td></tr><tr><td><a href="mysqlqb_functions.html#function_compress">COMPRESS()</a></td><td><a href="mysqlqb_functions.html#function_uncompress">UNCOMPRESS()</a></td></tr><tr><td><a href="mysqlqb_functions.html#function_encode">ENCODE()</a></td><td><a href="mysqlqb_functions.html#function_decode">DECODE()</a></td></tr><tr><td><a href="mysqlqb_functions.html#function_des-encrypt">DES_ENCRYPT()</a></td><td><a href="mysqlqb_functions.html#function_des-decrypt">DES_DECRYPT()</a></td></tr><tr><td><a href="mysqlqb_functions.html#function_encrypt">ENCRYPT()</a></td><td>Not available</td></tr><tr><td><a href="mysqlqb_functions.html#function_md5">MD5()</a></td><td>Not available</td></tr><tr><td><a href="mysqlqb_functions.html#function_old-password">OLD_PASSWORD()</a></td><td>Not available</td></tr><tr><td><a href="mysqlqb_functions.html#function_password">PASSWORD()</a></td><td>Not available</td></tr><tr><td><a href="mysqlqb_functions.html#function_sha1">SHA() or SHA1()</a></td><td>Not available</td></tr><tr><td>Not available</td><td><a href="mysqlqb_functions.html#function_uncompressed-length">UNCOMPRESSED_LENGTH()</a></td></tr></tbody></table></div><p>
<span class="bold"><strong>Note</strong></span>: The encryption and
compression functions return binary strings. For many of these
functions, the result might contain arbitrary byte values. If
you want to store these results, use a <code class="literal">BLOB</code>
column rather than a <code class="literal">CHAR</code> or (before MySQL
5.0.3) <code class="literal">VARCHAR</code> column to avoid potential
problems with trailing space removal that would change data
values.
</p><p>
<span class="bold"><strong>Note</strong></span>: Exploits for the MD5 and
SHA-1 algorithms have become known. You may wish to consider
using one of the other encryption functions described in this
section instead.
</p><div class="itemizedlist"><ul type="disc"><li><p><a id="function_aes-encrypt"></a>
<a id="id2849133" class="indexterm"></a>
<a id="id2849140" class="indexterm"></a>
<code class="literal">AES_ENCRYPT(<em class="replaceable"><code>str</code></em>,<em class="replaceable"><code>key_str</code></em>)</code>,
<code class="literal">AES_DECRYPT(<em class="replaceable"><code>crypt_str</code></em>,<em class="replaceable"><code>key_str</code></em>)</code>
</p><p>
These functions allow encryption and decryption of data
using the official AES (Advanced Encryption Standard)
algorithm, previously known as “<span class="quote">Rijndael.</span>”
Encoding with a 128-bit key length is used, but you can
extend it up to 256 bits by modifying the source. We chose
128 bits because it is much faster and it is secure enough
for most purposes.
</p><p>
<code class="literal">AES_ENCRYPT()</code> encrypts a string and
returns a binary string. <code class="literal">AES_DECRYPT()</code>
decrypts the encrypted string and returns the original
string. The input arguments may be any length. If either
argument is <code class="literal">NULL</code>, the result of this
function is also <code class="literal">NULL</code>.
</p><p>
Because AES is a block-level algorithm, padding is used to
encode uneven length strings and so the result string length
may be calculated using this formula:
</p><pre class="programlisting">16 × (trunc(<em class="replaceable"><code>string_length</code></em> / 16) + 1)
</pre><p>
If <code class="literal">AES_DECRYPT()</code> detects invalid data or
incorrect padding, it returns <code class="literal">NULL</code>.
However, it is possible for <code class="literal">AES_DECRYPT()</code>
to return a non-<code class="literal">NULL</code> value (possibly
garbage) if the input data or the key is invalid.
</p><p>
You can use the AES functions to store data in an encrypted
form by modifying your queries:
</p><pre class="programlisting">INSERT INTO t VALUES (1,AES_ENCRYPT('text','password'));
</pre><p>
<code class="literal">AES_ENCRYPT()</code> and
<code class="literal">AES_DECRYPT()</code> can be considered the most
cryptographically secure encryption functions currently
available in MySQL.
</p></li><li><p><a id="function_compress"></a>
<a id="id2849302" class="indexterm"></a>
<code class="literal">COMPRESS(<em class="replaceable"><code>string_to_compress</code></em>)</code>
</p><p>
Compresses a string and returns the result as a binary
string. This function requires MySQL to have been compiled
with a compression library such as <code class="literal">zlib</code>.
Otherwise, the return value is always
<code class="literal">NULL</code>. The compressed string can be
uncompressed with <code class="literal">UNCOMPRESS()</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT LENGTH(COMPRESS(REPEAT('a',1000)));</code></strong>
-> 21
mysql> <strong class="userinput"><code>SELECT LENGTH(COMPRESS(''));</code></strong>
-> 0
mysql> <strong class="userinput"><code>SELECT LENGTH(COMPRESS('a'));</code></strong>
-> 13
mysql> <strong class="userinput"><code>SELECT LENGTH(COMPRESS(REPEAT('a',16)));</code></strong>
-> 15
</pre><p>
The compressed string contents are stored the following way:
</p><div class="itemizedlist"><ul type="circle"><li><p>
Empty strings are stored as empty strings.
</p></li><li><p>
Non-empty strings are stored as a four-byte length of
the uncompressed string (low byte first), followed by
the compressed string. If the string ends with space, an
extra ‘<code class="literal">.</code>’ character is
added to avoid problems with endspace trimming should
the result be stored in a <code class="literal">CHAR</code> or
<code class="literal">VARCHAR</code> column. (Use of
<code class="literal">CHAR</code> or <code class="literal">VARCHAR</code> to
store compressed strings is not recommended. It is
better to use a <code class="literal">BLOB</code> column instead.)
</p></li></ul></div></li><li><p><a id="function_decode"></a>
<a id="id2849453" class="indexterm"></a>
<code class="literal">DECODE(<em class="replaceable"><code>crypt_str</code></em>,<em class="replaceable"><code>pass_str</code></em>)</code>
</p><p>
Decrypts the encrypted string
<em class="replaceable"><code>crypt_str</code></em> using
<em class="replaceable"><code>pass_str</code></em> as the password.
<em class="replaceable"><code>crypt_str</code></em> should be a string
returned from <code class="literal">ENCODE()</code>.
</p></li><li><p><a id="function_encode"></a>
<a id="id2849524" class="indexterm"></a>
<code class="literal">ENCODE(<em class="replaceable"><code>str</code></em>,<em class="replaceable"><code>pass_str</code></em>)</code>
</p><p>
Encrypt <em class="replaceable"><code>str</code></em> using
<em class="replaceable"><code>pass_str</code></em> as the password. To
decrypt the result, use <code class="literal">DECODE()</code>.
</p><p>
The result is a binary string of the same length as
<em class="replaceable"><code>str</code></em>.
</p><p>
The strength of the encryption is based on how good the
random generator is. It should suffice for short strings.
</p></li><li><p><a id="function_des-decrypt"></a>
<a id="id2849605" class="indexterm"></a>
<code class="literal">DES_DECRYPT(<em class="replaceable"><code>crypt_str</code></em>[,<em class="replaceable"><code>key_str</code></em>])</code>
</p><p>
Decrypts a string encrypted with
<code class="literal">DES_ENCRYPT()</code>. If an error occurs, this
function returns <code class="literal">NULL</code>.
</p><p>
Note that this function works only if MySQL has been
configured with SSL support. See
<a href="http://dev.mysql.com/doc/refman/5.0/en/secure-connections.html" target="_top">Using Secure Connections</a>.
</p><p>
If no <em class="replaceable"><code>key_str</code></em> argument is given,
<code class="literal">DES_DECRYPT()</code> examines the first byte of
the encrypted string to determine the DES key number that
was used to encrypt the original string, and then reads the
key from the DES key file to decrypt the message. For this
to work, the user must have the <code class="literal">SUPER</code>
privilege. The key file can be specified with the
<code class="option">--des-key-file</code> server option.
</p><p>
If you pass this function a
<em class="replaceable"><code>key_str</code></em> argument, that string is
used as the key for decrypting the message.
</p><p>
If the <em class="replaceable"><code>crypt_str</code></em> argument does
not appear to be an encrypted string, MySQL returns the
given <em class="replaceable"><code>crypt_str</code></em>.
</p></li><li><p><a id="function_des-encrypt"></a>
<a id="id2849729" class="indexterm"></a>
<code class="literal">DES_ENCRYPT(<em class="replaceable"><code>str</code></em>[,{<em class="replaceable"><code>key_num</code></em>|<em class="replaceable"><code>key_str</code></em>}])</code>
</p><p>
Encrypts the string with the given key using the Triple-DES
algorithm.
</p><p>
Note that this function works only if MySQL has been
configured with SSL support. See
<a href="http://dev.mysql.com/doc/refman/5.0/en/secure-connections.html" target="_top">Using Secure Connections</a>.
</p><p>
The encryption key to use is chosen based on the second
argument to <code class="literal">DES_ENCRYPT()</code>, if one was
given:
</p><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><tbody><tr><td><span class="bold"><strong>Argument</strong></span></td><td><span class="bold"><strong>Description</strong></span></td></tr><tr><td>No argument</td><td>The first key from the DES key file is used.</td></tr><tr><td><em class="replaceable"><code>key_num</code></em></td><td>The given key number (0-9) from the DES key file is used.</td></tr><tr><td><em class="replaceable"><code>key_str</code></em></td><td>The given key string is used to encrypt <em class="replaceable"><code>str</code></em>.</td></tr></tbody></table></div><p>
The key file can be specified with the
<code class="option">--des-key-file</code> server option.
</p><p>
The return string is a binary string where the first
character is <code class="literal">CHAR(128 |
<em class="replaceable"><code>key_num</code></em>)</code>. If an error
occurs, <code class="literal">DES_ENCRYPT()</code> returns
<code class="literal">NULL</code>.
</p><p>
The 128 is added to make it easier to recognize an encrypted
key. If you use a string key,
<em class="replaceable"><code>key_num</code></em> is 127.
</p><p>
The string length for the result is given by this formula:
</p><pre class="programlisting"><em class="replaceable"><code>new_len</code></em> = <em class="replaceable"><code>orig_len</code></em> + (8 - (<em class="replaceable"><code>orig_len</code></em> % 8)) + 1
</pre><p>
Each line in the DES key file has the following format:
</p><pre class="programlisting"><em class="replaceable"><code>key_num</code></em> <em class="replaceable"><code>des_key_str</code></em>
</pre><p>
Each <em class="replaceable"><code>key_num</code></em> value must be a
number in the range from <code class="literal">0</code> to
<code class="literal">9</code>. Lines in the file may be in any order.
<em class="replaceable"><code>des_key_str</code></em> is the string that is
used to encrypt the message. There should be at least one
space between the number and the key. The first key is the
default key that is used if you do not specify any key
argument to <code class="literal">DES_ENCRYPT()</code>.
</p><p>
You can tell MySQL to read new key values from the key file
with the <code class="literal">FLUSH DES_KEY_FILE</code> statement.
This requires the <code class="literal">RELOAD</code> privilege.
</p><p>
One benefit of having a set of default keys is that it gives
applications a way to check for the existence of encrypted
column values, without giving the end user the right to
decrypt those values.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT customer_address FROM customer_table </code></strong>
> <strong class="userinput"><code>WHERE crypted_credit_card = DES_ENCRYPT('credit_card_number');</code></strong>
</pre></li><li><p><a id="function_encrypt"></a>
<a id="id2850022" class="indexterm"></a>
<code class="literal">ENCRYPT(<em class="replaceable"><code>str</code></em>[,<em class="replaceable"><code>salt</code></em>])</code>
</p><p>
Encrypts <em class="replaceable"><code>str</code></em> using the Unix
<code class="literal">crypt()</code> system call and returns a binary
string. The <em class="replaceable"><code>salt</code></em> argument should
be a string with at least two characters. If no
<em class="replaceable"><code>salt</code></em> argument is given, a random
value is used.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT ENCRYPT('hello');</code></strong>
-> 'VxuFAJXVARROc'
</pre><p>
<code class="literal">ENCRYPT()</code> ignores all but the first eight
characters of <em class="replaceable"><code>str</code></em>, at least on
some systems. This behavior is determined by the
implementation of the underlying <code class="literal">crypt()</code>
system call.
</p><p>
The use of <code class="literal">ENCYPT()</code> with multi-byte
character sets other than <code class="literal">utf8</code> is not
recommended because the system call expects a string
terminated by a zero byte.
</p><p>
If <code class="literal">crypt()</code> is not available on your
system (as is the case with Windows),
<code class="literal">ENCRYPT()</code> always returns
<code class="literal">NULL</code>.
</p></li><li><p><a id="function_md5"></a>
<a id="id2850156" class="indexterm"></a>
<code class="literal">MD5(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
Calculates an MD5 128-bit checksum for the string. The value
is returned as a binary string of 32 hex digits, or
<code class="literal">NULL</code> if the argument was
<code class="literal">NULL</code>. The return value can, for example,
be used as a hash key.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT MD5('testing');</code></strong>
-> 'ae2b1fca515949e5d54fb22b8ed95575'
</pre><p>
This is the “<span class="quote">RSA Data Security, Inc. MD5
Message-Digest Algorithm.</span>”
</p><p>
If you want to convert the value to uppercase, see the
description of binary string conversion given in the entry
for the <code class="literal">BINARY</code> operator in
<a href="mysqlqb_functions.html#cast-functions" title="Cast Functions and Operators">Cast Functions and Operators</a>.
</p><p>
See the note regarding the MD5 algorithm at the beginning
this section.
</p></li><li><p><a id="function_old-password"></a>
<a id="id2850260" class="indexterm"></a>
<code class="literal">OLD_PASSWORD(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
<code class="literal">OLD_PASSWORD()</code> was added to MySQL when
the implementation of <code class="literal">PASSWORD()</code> was
changed to improve security.
<code class="literal">OLD_PASSWORD()</code> returns the value of the
old (pre-4.1) implementation of
<code class="literal">PASSWORD()</code> as a binary string, and is
intended to permit you to reset passwords for any pre-4.1
clients that need to connect to your version
5.0 MySQL server without locking them out. See
<a href="http://dev.mysql.com/doc/refman/5.0/en/password-hashing.html" target="_top">Password Hashing as of MySQL 4.1</a>.
</p></li><li><p><a id="function_password"></a>
<a id="id2850340" class="indexterm"></a>
<code class="literal">PASSWORD(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
Calculates and returns a password string from the plaintext
password <em class="replaceable"><code>str</code></em> and returns a binary
string, or <code class="literal">NULL</code> if the argument was
<code class="literal">NULL</code>. This is the function that is used
for encrypting MySQL passwords for storage in the
<code class="literal">Password</code> column of the
<code class="literal">user</code> grant table.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT PASSWORD('badpwd');</code></strong>
-> '*AAB3E285149C0135D51A520E1940DD3263DC008C'
</pre><p>
<a id="id2850411" class="indexterm"></a>
<code class="literal">PASSWORD()</code> encryption is one-way (not
reversible).
</p><p>
<code class="literal">PASSWORD()</code> does not perform password
encryption in the same way that Unix passwords are
encrypted. See <code class="literal">ENCRYPT()</code>.
</p><p>
<span class="bold"><strong>Note</strong></span>: The
<code class="literal">PASSWORD()</code> function is used by the
authentication system in MySQL Server; you should
<span class="emphasis"><em>not</em></span> use it in your own applications.
For that purpose, consider <code class="literal">MD5()</code> or
<code class="literal">SHA1()</code> instead. Also see
<a href="http://rfc.net/rfc2195.html" target="_top">RFC 2195, section 2
(Challenge-Response Authentication Mechanism
(CRAM))</a>, for more information about handling
passwords and authentication securely in your applications.
</p></li><li><p><a id="function_sha1"></a>
<a id="id2850502" class="indexterm"></a>
<a id="id2850508" class="indexterm"></a>
<code class="literal">SHA1(<em class="replaceable"><code>str</code></em>)</code>,
<code class="literal">SHA(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
Calculates an SHA-1 160-bit checksum for the string, as
described in RFC 3174 (Secure Hash Algorithm). The value is
returned as a binary string of 40 hex digits, or
<code class="literal">NULL</code> if the argument was
<code class="literal">NULL</code>. One of the possible uses for this
function is as a hash key. You can also use it as a
cryptographic function for storing passwords.
<code class="literal">SHA()</code> is synonymous with
<code class="literal">SHA1()</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT SHA1('abc');</code></strong>
-> 'a9993e364706816aba3e25717850c26c9cd0d89d'
</pre><p>
<code class="literal">SHA1()</code> can be considered a
cryptographically more secure equivalent of
<code class="literal">MD5()</code>. However, see the note regarding
the MD5 and SHA-1 algorithms at the beginning this section.
</p></li><li><p><a id="function_uncompress"></a>
<a id="id2850616" class="indexterm"></a>
<code class="literal">UNCOMPRESS(<em class="replaceable"><code>string_to_uncompress</code></em>)</code>
</p><p>
Uncompresses a string compressed by the
<code class="literal">COMPRESS()</code> function. If the argument is
not a compressed value, the result is
<code class="literal">NULL</code>. This function requires MySQL to
have been compiled with a compression library such as
<code class="literal">zlib</code>. Otherwise, the return value is
always <code class="literal">NULL</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT UNCOMPRESS(COMPRESS('any string'));</code></strong>
-> 'any string'
mysql> <strong class="userinput"><code>SELECT UNCOMPRESS('any string');</code></strong>
-> NULL
</pre></li><li><p><a id="function_uncompressed-length"></a>
<a id="id2850706" class="indexterm"></a>
<code class="literal">UNCOMPRESSED_LENGTH(<em class="replaceable"><code>compressed_string</code></em>)</code>
</p><p>
Returns the length that the compressed string had before
being compressed.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT UNCOMPRESSED_LENGTH(COMPRESS(REPEAT('a',30)));</code></strong>
-> 30
</pre></li></ul></div></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="information-functions"></a>Information Functions</h3></div></div></div><a id="id2850765" class="indexterm"></a><a id="id2850772" class="indexterm"></a><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><thead><tr><th><span class="bold"><strong>Name</strong></span></th><th><span class="bold"><strong>Description</strong></span></th></tr></thead><tbody><tr><td><a href="mysqlqb_functions.html#function_benchmark"><code class="literal">BENCHMARK()</code></a></td><td>Repeatedly execute an expression</td></tr><tr><td><a href="mysqlqb_functions.html#function_charset"><code class="literal">CHARSET()</code></a></td><td>Return the character set of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_coercibility"><code class="literal">COERCIBILITY()</code></a></td><td>Return the collation coercibility value of the string argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_collation"><code class="literal">COLLATION()</code></a></td><td>Return the collation of the string argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_connection-id"><code class="literal">CONNECTION_ID()</code></a></td><td>Return the connection ID (thread ID) for the connection</td></tr><tr><td><a href="mysqlqb_functions.html#function_current-user"><code class="literal">CURRENT_USER()</code>, <code class="literal">CURRENT_USER</code></a></td><td>Return the username and hostname combination</td></tr><tr><td><a href="mysqlqb_functions.html#function_database"><code class="literal">DATABASE()</code></a></td><td>Return the default (current) database name</td></tr><tr><td><a href="mysqlqb_functions.html#function_found-rows"><code class="literal">FOUND_ROWS()</code></a></td><td>For a SELECT with a LIMIT clause, the number of rows that would be returned were there no LIMIT clause</td></tr><tr><td><a href="mysqlqb_functions.html#function_last-insert-id"><code class="literal">LAST_INSERT_ID()</code></a></td><td>Value of the AUTOINCREMENT column for the last INSERT</td></tr><tr><td><a href="mysqlqb_functions.html#function_row-count"><code class="literal">ROW_COUNT()</code></a></td><td>The number of rows updated</td></tr><tr><td><a href="mysqlqb_functions.html#function_schema"><code class="literal">SCHEMA()</code></a></td><td>A synonym for DATABASE()</td></tr><tr><td><a href="mysqlqb_functions.html#function_session-user"><code class="literal">SESSION_USER()</code></a></td><td>Synonym for USER()</td></tr><tr><td><a href="mysqlqb_functions.html#function_system-user"><code class="literal">SYSTEM_USER()</code></a></td><td>Synonym for USER()</td></tr><tr><td><a href="mysqlqb_functions.html#function_user"><code class="literal">USER()</code></a></td><td>Return the current username and hostname</td></tr></tbody></table></div><div class="itemizedlist"><ul type="disc"><li><p><a id="function_benchmark"></a>
<a id="id2851008" class="indexterm"></a>
<code class="literal">BENCHMARK(<em class="replaceable"><code>count</code></em>,<em class="replaceable"><code>expr</code></em>)</code>
</p><p>
The <code class="literal">BENCHMARK()</code> function executes the
expression <em class="replaceable"><code>expr</code></em> repeatedly
<em class="replaceable"><code>count</code></em> times. It may be used to
time how quickly MySQL processes the expression. The result
value is always <code class="literal">0</code>. The intended use is
from within the <span><strong class="command">mysql</strong></span> client, which
reports query execution times:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT BENCHMARK(1000000,ENCODE('hello','goodbye'));</code></strong>
+----------------------------------------------+
| BENCHMARK(1000000,ENCODE('hello','goodbye')) |
+----------------------------------------------+
| 0 |
+----------------------------------------------+
1 row in set (4.74 sec)
</pre><p>
The time reported is elapsed time on the client end, not CPU
time on the server end. It is advisable to execute
<code class="literal">BENCHMARK()</code> several times, and to
interpret the result with regard to how heavily loaded the
server machine is.
</p><p>
<code class="literal">BENCHMARK()</code> is intended for measuring the
runtime performance of scalar expressions, which has some
significant implications for the way that you use it and
interpret the results:
</p><div class="itemizedlist"><ul type="circle"><li><p>
Only scalar expressions can be used. Although the
expression can be a subquery, it must return a single
column and at most a single row. For example,
<code class="literal">BENCHMARK(10, (SELECT * FROM t))</code> will
fail if the table <code class="literal">t</code> has more than one
column or more than one row.
</p></li><li><p>
Executing a <code class="literal">SELECT
<em class="replaceable"><code>expr</code></em></code> statement
<em class="replaceable"><code>N</code></em> times differs from
executing <code class="literal">SELECT
BENCHMARK(<em class="replaceable"><code>N</code></em>,
<em class="replaceable"><code>expr</code></em>)</code> in terms of
the amount of overhead involved. The two have very
different execution profiles and you should not expect
them to take the same amount of time. The former
involves the parser, optimizer, table locking, and
runtime evaluation <em class="replaceable"><code>N</code></em> times
each. The latter involves only runtime evaluation
<em class="replaceable"><code>N</code></em> times, and all the other
components just once. Memory structures already
allocated are reused, and runtime optimizations such as
local cacheing of results already evaluated for
aggregate functions can alter the results. Use of
<code class="literal">BENCHMARK()</code> thus measures performance
of the runtime component by giving more weight to that
component and removing the “<span class="quote">noise</span>”
introduced by the network, parser, optimizer, and so
forth.
</p></li></ul></div></li><li><p><a id="function_charset"></a>
<a id="id2814531" class="indexterm"></a>
<code class="literal">CHARSET(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
Returns the character set of the string argument.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT CHARSET('abc');</code></strong>
-> 'latin1'
mysql> <strong class="userinput"><code>SELECT CHARSET(CONVERT('abc' USING utf8));</code></strong>
-> 'utf8'
mysql> <strong class="userinput"><code>SELECT CHARSET(USER());</code></strong>
-> 'utf8'
</pre></li><li><p><a id="function_coercibility"></a>
<a id="id2814604" class="indexterm"></a>
<code class="literal">COERCIBILITY(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
Returns the collation coercibility value of the string
argument.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT COERCIBILITY('abc' COLLATE latin1_swedish_ci);</code></strong>
-> 0
mysql> <strong class="userinput"><code>SELECT COERCIBILITY(USER());</code></strong>
-> 3
mysql> <strong class="userinput"><code>SELECT COERCIBILITY('abc');</code></strong>
-> 4
</pre><p>
The return values have the meanings shown in the following
table. Lower values have higher precedence.
</p><div class="informaltable"><table border="1"><colgroup><col /><col /><col /></colgroup><tbody><tr><td><span class="bold"><strong>Coercibility</strong></span></td><td><span class="bold"><strong>Meaning</strong></span></td><td><span class="bold"><strong>Example</strong></span></td></tr><tr><td><code class="literal">0</code></td><td>Explicit collation</td><td>Value with <code class="literal">COLLATE</code> clause</td></tr><tr><td><code class="literal">1</code></td><td>No collation</td><td>Concatenation of strings with different collations</td></tr><tr><td><code class="literal">2</code></td><td>Implicit collation</td><td>Column value</td></tr><tr><td><code class="literal">3</code></td><td>System constant</td><td><code class="literal">USER()</code> return value</td></tr><tr><td><code class="literal">4</code></td><td>Coercible</td><td>Literal string</td></tr><tr><td><code class="literal">5</code></td><td>Ignorable</td><td><code class="literal">NULL</code> or an expression derived from
<code class="literal">NULL</code></td></tr></tbody></table></div><p>
Before MySQL 5.0.3, the return values are shown as follows,
and functions such as <code class="literal">USER()</code> have a
coercibility of 2:
</p><div class="informaltable"><table border="1"><colgroup><col /><col /><col /></colgroup><tbody><tr><td><span class="bold"><strong>Coercibility</strong></span></td><td><span class="bold"><strong>Meaning</strong></span></td><td><span class="bold"><strong>Example</strong></span></td></tr><tr><td><code class="literal">0</code></td><td>Explicit collation</td><td>Value with <code class="literal">COLLATE</code> clause</td></tr><tr><td><code class="literal">1</code></td><td>No collation</td><td>Concatenation of strings with different collations</td></tr><tr><td><code class="literal">2</code></td><td>Implicit collation</td><td>Column value, stored routine parameter or local variable</td></tr><tr><td><code class="literal">3</code></td><td>Coercible</td><td>Literal string</td></tr></tbody></table></div></li><li><p><a id="function_collation"></a>
<a id="id2851346" class="indexterm"></a>
<code class="literal">COLLATION(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
Returns the collation of the string argument.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT COLLATION('abc');</code></strong>
-> 'latin1_swedish_ci'
mysql> <strong class="userinput"><code>SELECT COLLATION(_utf8'abc');</code></strong>
-> 'utf8_general_ci'
</pre></li><li><p><a id="function_connection-id"></a>
<a id="id2851415" class="indexterm"></a>
<code class="literal">CONNECTION_ID()</code>
</p><p>
Returns the connection ID (thread ID) for the connection.
Every connection has an ID that is unique among the set of
currently connected clients.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT CONNECTION_ID();</code></strong>
-> 23786
</pre></li><li><p><a id="function_current-user"></a>
<a id="id2851476" class="indexterm"></a>
<code class="literal">CURRENT_USER</code>,
<code class="literal">CURRENT_USER()</code>
</p><p>
Returns the username and hostname combination for the MySQL
account that the server used to authenticate the current
client. This account determines your access privileges. As
of MySQL 5.0.10, within a stored routine that is defined
with the <code class="literal">SQL SECURITY DEFINER</code>
characteristic, <code class="literal">CURRENT_USER()</code> returns
the creator of the routine. The return value is a string in
the <code class="literal">utf8</code> character set.
</p><p>
The value of <code class="literal">CURRENT_USER()</code> can differ
from the value of <code class="literal">USER()</code>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT USER();</code></strong>
-> 'davida@localhost'
mysql> <strong class="userinput"><code>SELECT * FROM mysql.user;</code></strong>
ERROR 1044: Access denied for user ''@'localhost' to
database 'mysql'
mysql> <strong class="userinput"><code>SELECT CURRENT_USER();</code></strong>
-> '@localhost'
</pre><p>
The example illustrates that although the client specified a
username of <code class="literal">davida</code> (as indicated by the
value of the <code class="literal">USER()</code> function), the server
authenticated the client using an anonymous user account (as
seen by the empty username part of the
<code class="literal">CURRENT_USER()</code> value). One way this might
occur is that there is no account listed in the grant tables
for <code class="literal">davida</code>.
</p></li><li><p><a id="function_database"></a>
<a id="id2851603" class="indexterm"></a>
<code class="literal">DATABASE()</code>
</p><p>
Returns the default (current) database name as a string in
the <code class="literal">utf8</code> character set. If there is no
default database, <code class="literal">DATABASE()</code> returns
<code class="literal">NULL</code>. Within a stored routine, the
default database is the database that the routine is
associated with, which is not necessarily the same as the
database that is the default in the calling context.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT DATABASE();</code></strong>
-> 'test'
</pre></li><li><p><a id="function_found-rows"></a>
<a id="id2851678" class="indexterm"></a>
<a id="id2851685" class="indexterm"></a>
<code class="literal">FOUND_ROWS()</code>
</p><p>
A <code class="literal">SELECT</code> statement may include a
<code class="literal">LIMIT</code> clause to restrict the number of
rows the server returns to the client. In some cases, it is
desirable to know how many rows the statement would have
returned without the <code class="literal">LIMIT</code>, but without
running the statement again. To obtain this row count,
include a <code class="literal">SQL_CALC_FOUND_ROWS</code> option in
the <code class="literal">SELECT</code> statement, and then invoke
<code class="literal">FOUND_ROWS()</code> afterward:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT SQL_CALC_FOUND_ROWS * FROM <em class="replaceable"><code>tbl_name</code></em></code></strong>
-> <strong class="userinput"><code>WHERE id > 100 LIMIT 10;</code></strong>
mysql> <strong class="userinput"><code>SELECT FOUND_ROWS();</code></strong>
</pre><p>
The second <code class="literal">SELECT</code> returns a number
indicating how many rows the first <code class="literal">SELECT</code>
would have returned had it been written without the
<code class="literal">LIMIT</code> clause.
</p><p>
In the absence of the <code class="literal">SQL_CALC_FOUND_ROWS</code>
option in the most recent <code class="literal">SELECT</code>
statement, <code class="literal">FOUND_ROWS()</code> returns the
number of rows in the result set returned by that statement.
</p><p>
The row count available through
<code class="literal">FOUND_ROWS()</code> is transient and not
intended to be available past the statement following the
<code class="literal">SELECT SQL_CALC_FOUND_ROWS</code> statement. If
you need to refer to the value later, save it:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT SQL_CALC_FOUND_ROWS * FROM ... ;</code></strong>
mysql> <strong class="userinput"><code>SET @rows = FOUND_ROWS();</code></strong>
</pre><p>
If you are using <code class="literal">SELECT
SQL_CALC_FOUND_ROWS</code>, MySQL must calculate how many
rows are in the full result set. However, this is faster
than running the query again without
<code class="literal">LIMIT</code>, because the result set need not be
sent to the client.
</p><p>
<code class="literal">SQL_CALC_FOUND_ROWS</code> and
<code class="literal">FOUND_ROWS()</code> can be useful in situations
when you want to restrict the number of rows that a query
returns, but also determine the number of rows in the full
result set without running the query again. An example is a
Web script that presents a paged display containing links to
the pages that show other sections of a search result. Using
<code class="literal">FOUND_ROWS()</code> allows you to determine how
many other pages are needed for the rest of the result.
</p><p>
The use of <code class="literal">SQL_CALC_FOUND_ROWS</code> and
<code class="literal">FOUND_ROWS()</code> is more complex for
<code class="literal">UNION</code> statements than for simple
<code class="literal">SELECT</code> statements, because
<code class="literal">LIMIT</code> may occur at multiple places in a
<code class="literal">UNION</code>. It may be applied to individual
<code class="literal">SELECT</code> statements in the
<code class="literal">UNION</code>, or global to the
<code class="literal">UNION</code> result as a whole.
</p><p>
The intent of <code class="literal">SQL_CALC_FOUND_ROWS</code> for
<code class="literal">UNION</code> is that it should return the row
count that would be returned without a global
<code class="literal">LIMIT</code>. The conditions for use of
<code class="literal">SQL_CALC_FOUND_ROWS</code> with
<code class="literal">UNION</code> are:
</p><div class="itemizedlist"><ul type="circle"><li><p>
The <code class="literal">SQL_CALC_FOUND_ROWS</code> keyword must
appear in the first <code class="literal">SELECT</code> of the
<code class="literal">UNION</code>.
</p></li><li><p>
The value of <code class="literal">FOUND_ROWS()</code> is exact
only if <code class="literal">UNION ALL</code> is used. If
<code class="literal">UNION</code> without <code class="literal">ALL</code>
is used, duplicate removal occurs and the value of
<code class="literal">FOUND_ROWS()</code> is only approximate.
</p></li><li><p>
If no <code class="literal">LIMIT</code> is present in the
<code class="literal">UNION</code>,
<code class="literal">SQL_CALC_FOUND_ROWS</code> is ignored and
returns the number of rows in the temporary table that
is created to process the <code class="literal">UNION</code>.
</p></li></ul></div></li><li><p><a id="function_last-insert-id"></a>
<a id="id2852016" class="indexterm"></a>
<code class="literal">LAST_INSERT_ID()</code>,
<code class="literal">LAST_INSERT_ID(<em class="replaceable"><code>expr</code></em>)</code>
</p><p>
<code class="literal">LAST_INSERT_ID()</code> (with no argument)
returns the <span class="emphasis"><em>first</em></span> automatically
generated value that was set for an
<code class="literal">AUTO_INCREMENT</code> column by the
<span class="emphasis"><em>most recently executed</em></span>
<code class="literal">INSERT</code> statement to affect such a column.
For example, after inserting a row that generates an
<code class="literal">AUTO_INCREMENT</code> value, you can get the
value like this:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT LAST_INSERT_ID();</code></strong>
-> 195
</pre><p>
The currently executing statement does not affect the value
of <code class="literal">LAST_INSERT_ID()</code>. Suppose that you
generate an <code class="literal">AUTO_INCREMENT</code> value with one
statement, and then refer to
<code class="literal">LAST_INSERT_ID()</code> in a multiple-row
<code class="literal">INSERT</code> statement that inserts rows into a
table with its own <code class="literal">AUTO_INCREMENT</code> column.
The value of <code class="literal">LAST_INSERT_ID()</code> will remain
stable in the second statement; its value for the second and
later rows is not affected by the earlier row insertions.
(However, if you mix references to
<code class="literal">LAST_INSERT_ID()</code> and
<code class="literal">LAST_INSERT_ID(<em class="replaceable"><code>expr</code></em>)</code>,
the effect is undefined.)
</p><p>
If the previous statement returned an error, the value of
<code class="literal">LAST_INSERT_ID()</code> is undefined. For
transactional tables, if the statement is rolled back due to
an error, the value of <code class="literal">LAST_INSERT_ID()</code>
is left undefined. For manual <code class="literal">ROLLBACK</code>,
the value of <code class="literal">LAST_INSERT_ID()</code> is not
restored to that before the transaction; it remains as it
was at the point of the <code class="literal">ROLLBACK</code>.
</p><p>
Within the body of a stored routine (procedure or function)
or a trigger, the value of
<code class="literal">LAST_INSERT_ID()</code> changes the same way as
for statements executed outside the body of these kinds of
objects. The effect of a stored routine or trigger upon the
value of <code class="literal">LAST_INSERT_ID()</code> that is seen by
following statements depends on the kind of routine:
</p><div class="itemizedlist"><ul type="circle"><li><p>
If a stored procedure executes statements that change
the value of <code class="literal">LAST_INSERT_ID()</code>, the
changed value will be seen by statements that follow the
procedure call.
</p></li><li><p>
For stored functions and triggers that change the value,
the value is restored when the function or trigger ends,
so following statements will not see a changed value.
</p></li></ul></div><p>
The ID that was generated is maintained in the server on a
<span class="emphasis"><em>per-connection basis</em></span>. This means that
the value returned by the function to a given client is the
first <code class="literal">AUTO_INCREMENT</code> value generated for
most recent statement affecting an
<code class="literal">AUTO_INCREMENT</code> column <span class="emphasis"><em>by that
client</em></span>. This value cannot be affected by other
clients, even if they generate
<code class="literal">AUTO_INCREMENT</code> values of their own. This
behavior ensures that each client can retrieve its own ID
without concern for the activity of other clients, and
without the need for locks or transactions.
</p><p>
The value of <code class="literal">LAST_INSERT_ID()</code> is not
changed if you set the <code class="literal">AUTO_INCREMENT</code>
column of a row to a non-“<span class="quote">magic</span>” value (that
is, a value that is not <code class="literal">NULL</code> and not
<code class="literal">0</code>).
</p><p>
<span class="bold"><strong>Important</strong></span>: If you insert
multiple rows using a single <code class="literal">INSERT</code>
statement, <code class="literal">LAST_INSERT_ID()</code> returns the
value generated for the <span class="emphasis"><em>first</em></span> inserted
row <span class="emphasis"><em>only</em></span>. The reason for this is to
make it possible to reproduce easily the same
<code class="literal">INSERT</code> statement against some other
server.
</p><p>
For example:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>USE test;</code></strong>
Database changed
mysql> <strong class="userinput"><code>CREATE TABLE t (</code></strong>
-> <strong class="userinput"><code>id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,</code></strong>
-> <strong class="userinput"><code>name VARCHAR(10) NOT NULL</code></strong>
-> <strong class="userinput"><code>);</code></strong>
Query OK, 0 rows affected (0.09 sec)
mysql> <strong class="userinput"><code>INSERT INTO t VALUES (NULL, 'Bob');</code></strong>
Query OK, 1 row affected (0.01 sec)
mysql> <strong class="userinput"><code>SELECT * FROM t;</code></strong>
+----+------+
| id | name |
+----+------+
| 1 | Bob |
+----+------+
1 row in set (0.01 sec)
mysql> <strong class="userinput"><code>SELECT LAST_INSERT_ID();</code></strong>
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 1 |
+------------------+
1 row in set (0.00 sec)
mysql> <strong class="userinput"><code>INSERT INTO t VALUES</code></strong>
-> <strong class="userinput"><code>(NULL, 'Mary'), (NULL, 'Jane'), (NULL, 'Lisa');</code></strong>
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM t;
+----+------+
| id | name |
+----+------+
| 1 | Bob |
| 2 | Mary |
| 3 | Jane |
| 4 | Lisa |
+----+------+
4 rows in set (0.01 sec)
mysql> <strong class="userinput"><code>SELECT LAST_INSERT_ID();</code></strong>
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 2 |
+------------------+
1 row in set (0.00 sec)
</pre><p>
Although the second <code class="literal">INSERT</code> statement
inserted three new rows into <code class="literal">t</code>, the ID
generated for the first of these rows was
<code class="literal">2</code>, and it is this value that is returned
by <code class="literal">LAST_INSERT_ID()</code> for the following
<code class="literal">SELECT</code> statement.
</p><p>
If you use <code class="literal">INSERT IGNORE</code> and the row is
ignored, the <code class="literal">AUTO_INCREMENT</code> counter is
not incremented and <code class="literal">LAST_INSERT_ID()</code>
returns <code class="literal">0</code>, which reflects that no row was
inserted.
</p><p>
<a id="id2852422" class="indexterm"></a>
If <em class="replaceable"><code>expr</code></em> is given as an argument
to <code class="literal">LAST_INSERT_ID()</code>, the value of the
argument is returned by the function and is remembered as
the next value to be returned by
<code class="literal">LAST_INSERT_ID()</code>. This can be used to
simulate sequences:
</p><div class="orderedlist"><ol type="1"><li><p>
Create a table to hold the sequence counter and
initialize it:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>CREATE TABLE sequence (id INT NOT NULL);</code></strong>
mysql> <strong class="userinput"><code>INSERT INTO sequence VALUES (0);</code></strong>
</pre></li><li><p>
Use the table to generate sequence numbers like this:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>UPDATE sequence SET id=LAST_INSERT_ID(id+1);</code></strong>
mysql> <strong class="userinput"><code>SELECT LAST_INSERT_ID();</code></strong>
</pre><p>
The <code class="literal">UPDATE</code> statement increments the
sequence counter and causes the next call to
<code class="literal">LAST_INSERT_ID()</code> to return the
updated value. The <code class="literal">SELECT</code> statement
retrieves that value. The
<code class="literal">mysql_insert_id()</code> C API function can
also be used to get the value. See
<a href="http://dev.mysql.com/doc/refman/5.0/en/mysql-insert-id.html" target="_top"><code class="literal">mysql_insert_id()</code></a>.
</p></li></ol></div><p>
You can generate sequences without calling
<code class="literal">LAST_INSERT_ID()</code>, but the utility of
using the function this way is that the ID value is
maintained in the server as the last automatically generated
value. It is multi-user safe because multiple clients can
issue the <code class="literal">UPDATE</code> statement and get their
own sequence value with the <code class="literal">SELECT</code>
statement (or <code class="literal">mysql_insert_id()</code>), without
affecting or being affected by other clients that generate
their own sequence values.
</p><p>
Note that <code class="literal">mysql_insert_id()</code> is only
updated after <code class="literal">INSERT</code> and
<code class="literal">UPDATE</code> statements, so you cannot use the
C API function to retrieve the value for
<code class="literal">LAST_INSERT_ID(<em class="replaceable"><code>expr</code></em>)</code>
after executing other SQL statements like
<code class="literal">SELECT</code> or <code class="literal">SET</code>.
</p></li><li><p><a id="function_row-count"></a>
<a id="id2852599" class="indexterm"></a>
<code class="literal">ROW_COUNT()</code>
</p><p>
<code class="literal">ROW_COUNT()</code> returns the number of rows
updated, inserted, or deleted by the preceding statement.
This is the same as the row count that the
<span><strong class="command">mysql</strong></span> client displays and the value from
the <code class="literal">mysql_affected_rows()</code> C API function.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>INSERT INTO t VALUES(1),(2),(3);</code></strong>
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> <strong class="userinput"><code>SELECT ROW_COUNT();</code></strong>
+-------------+
| ROW_COUNT() |
+-------------+
| 3 |
+-------------+
1 row in set (0.00 sec)
mysql> <strong class="userinput"><code>DELETE FROM t WHERE i IN(1,2);</code></strong>
Query OK, 2 rows affected (0.00 sec)
mysql> <strong class="userinput"><code>SELECT ROW_COUNT();</code></strong>
+-------------+
| ROW_COUNT() |
+-------------+
| 2 |
+-------------+
1 row in set (0.00 sec)
</pre><p>
<code class="literal">ROW_COUNT()</code> was added in MySQL 5.0.1.
</p></li><li><p><a id="function_schema"></a>
<a id="id2852702" class="indexterm"></a>
<code class="literal">SCHEMA()</code>
</p><p>
This function is a synonym for
<code class="literal">DATABASE()</code>. It was added in MySQL 5.0.2.
</p></li><li><p><a id="function_session-user"></a>
<a id="id2852755" class="indexterm"></a>
<code class="literal">SESSION_USER()</code>
</p><p>
<code class="literal">SESSION_USER()</code> is a synonym for
<code class="literal">USER()</code>.
</p></li><li><p><a id="function_system-user"></a>
<a id="id2852811" class="indexterm"></a>
<code class="literal">SYSTEM_USER()</code>
</p><p>
<code class="literal">SYSTEM_USER()</code> is a synonym for
<code class="literal">USER()</code>.
</p></li><li><p><a id="function_user"></a>
<a id="id2852866" class="indexterm"></a>
<code class="literal">USER()</code>
</p><p>
Returns the current MySQL username and hostname as a string
in the <code class="literal">utf8</code> character set.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT USER();</code></strong>
-> 'davida@localhost'
</pre><p>
The value indicates the username you specified when
connecting to the server, and the client host from which you
connected. The value can be different from that of
<code class="literal">CURRENT_USER()</code>.
</p><p>
You can extract only the username part like this:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT SUBSTRING_INDEX(USER(),'@',1);</code></strong>
-> 'davida'
</pre></li><li><p><a id="function_version"></a>
<a id="id2852956" class="indexterm"></a>
<code class="literal">VERSION()</code>
</p><p>
Returns a string that indicates the MySQL server version.
The string uses the <code class="literal">utf8</code> character set.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT VERSION();</code></strong>
-> '5.0.38-standard'
</pre><p>
Note that if your version string ends with
<code class="literal">-log</code> this means that logging is enabled.
</p></li></ul></div></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="miscellaneous-functions"></a>Miscellaneous Functions</h3></div></div></div><a id="id2853024" class="indexterm"></a><a id="id2853031" class="indexterm"></a><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><thead><tr><th><span class="bold"><strong>Name</strong></span></th><th><span class="bold"><strong>Description</strong></span></th></tr></thead><tbody><tr><td><a href="mysqlqb_functions.html#function_count"><code class="literal">COUNT()</code></a></td><td>Return a count of the number of rows returned</td></tr><tr><td><a href="mysqlqb_functions.html#function_default"><code class="literal">DEFAULT()</code></a></td><td>Return the default value for a table column</td></tr><tr><td><a href="mysqlqb_functions.html#function_format"><code class="literal">FORMAT()</code></a></td><td>Return a number formatted to specified number of decimal places</td></tr><tr><td><a href="mysqlqb_functions.html#function_get-lock"><code class="literal">GET_LOCK()</code></a></td><td>Get a named lock</td></tr><tr><td><a href="mysqlqb_functions.html#function_inet-aton"><code class="literal">INET_ATON()</code></a></td><td>Return the numeric value of an IP address</td></tr><tr><td><a href="mysqlqb_functions.html#function_inet-ntoa"><code class="literal">INET_NTOA()</code></a></td><td>Return the IP address from a numeric value</td></tr><tr><td><a href="mysqlqb_functions.html#function_is-free-lock"><code class="literal">IS_FREE_LOCK()</code></a></td><td>Checks whether the named lock is free</td></tr><tr><td><a href="mysqlqb_functions.html#function_is-used-lock"><code class="literal">IS_USED_LOCK()</code></a></td><td>Checks whether the named lock is in use. Return connection identifier if true.</td></tr><tr><td><a href="mysqlqb_functions.html#function_master-pos-wait"><code class="literal">MASTER_POS_WAIT()</code></a></td><td>Block until the slave has read and applied all updates up to the specified position</td></tr><tr><td><a href="mysqlqb_functions.html#function_name-const"><code class="literal">NAME_CONST()</code></a>(v5.0.12)</td><td>Causes the column to have the given name</td></tr><tr><td><a href="mysqlqb_functions.html#function_release-lock"><code class="literal">RELEASE_LOCK()</code></a></td><td>Releases the named lock</td></tr><tr><td><a href="mysqlqb_functions.html#function_sleep"><code class="literal">SLEEP()</code></a></td><td>Sleep for a number of seconds</td></tr><tr><td><a href="mysqlqb_functions.html#function_uuid"><code class="literal">UUID()</code></a></td><td>Return a Universal Unique Identifier (UUID)</td></tr><tr><td><a href="mysqlqb_functions.html#function_values"><code class="literal">VALUES()</code></a></td><td>Defines the values to be used during an INSERT</td></tr></tbody></table></div><div class="itemizedlist"><ul type="disc"><li><p><a id="function_default"></a>
<a id="id2853265" class="indexterm"></a>
<code class="literal">DEFAULT(<em class="replaceable"><code>col_name</code></em>)</code>
</p><p>
Returns the default value for a table column. Starting with
MySQL 5.0.2, an error results if the column has no default
value.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>UPDATE t SET i = DEFAULT(i)+1 WHERE id < 100;</code></strong>
</pre></li><li><p>
<code class="literal">FORMAT(<em class="replaceable"><code>X</code></em>,<em class="replaceable"><code>D</code></em>)</code>
</p><p>
Formats the number <em class="replaceable"><code>X</code></em> to a format
like <code class="literal">'#,###,###.##'</code>, rounded to
<em class="replaceable"><code>D</code></em> decimal places, and returns the
result as a string. For details, see
<a href="mysqlqb_functions.html#string-functions" title="String Functions">String Functions</a>.
</p></li><li><p><a id="function_get-lock"></a>
<a id="id2853378" class="indexterm"></a>
<a id="id2853385" class="indexterm"></a>
<code class="literal">GET_LOCK(<em class="replaceable"><code>str</code></em>,<em class="replaceable"><code>timeout</code></em>)</code>
</p><p>
Tries to obtain a lock with a name given by the string
<em class="replaceable"><code>str</code></em>, using a timeout of
<em class="replaceable"><code>timeout</code></em> seconds. Returns
<code class="literal">1</code> if the lock was obtained successfully,
<code class="literal">0</code> if the attempt timed out (for example,
because another client has previously locked the name), or
<code class="literal">NULL</code> if an error occurred (such as
running out of memory or the thread was killed with
<span><strong class="command">mysqladmin kill</strong></span>). If you have a lock
obtained with <code class="literal">GET_LOCK()</code>, it is released
when you execute <code class="literal">RELEASE_LOCK()</code>, execute
a new <code class="literal">GET_LOCK()</code>, or your connection
terminates (either normally or abnormally). Locks obtained
with <code class="literal">GET_LOCK()</code> do not interact with
transactions. That is, committing a transaction does not
release any such locks obtained during the transaction.
</p><p>
This function can be used to implement application locks or
to simulate record locks. Names are locked on a server-wide
basis. If a name has been locked by one client,
<code class="literal">GET_LOCK()</code> blocks any request by another
client for a lock with the same name. This allows clients
that agree on a given lock name to use the name to perform
cooperative advisory locking. But be aware that it also
allows a client that is not among the set of cooperating
clients to lock a name, either inadvertently or
deliberately, and thus prevent any of the cooperating
clients from locking that name. One way to reduce the
likelihood of this is to use lock names that are
database-specific or application-specific. For example, use
lock names of the form
<em class="replaceable"><code>db_name.str</code></em> or
<em class="replaceable"><code>app_name.str</code></em>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT GET_LOCK('lock1',10);</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT IS_FREE_LOCK('lock2');</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT GET_LOCK('lock2',10);</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT RELEASE_LOCK('lock2');</code></strong>
-> 1
mysql> <strong class="userinput"><code>SELECT RELEASE_LOCK('lock1');</code></strong>
-> NULL
</pre><p>
The second <code class="literal">RELEASE_LOCK()</code> call returns
<code class="literal">NULL</code> because the lock
<code class="literal">'lock1'</code> was automatically released by the
second <code class="literal">GET_LOCK()</code> call.
</p><p>
Note: If a client attempts to acquire a lock that is already
held by another client, it blocks according to the
<em class="replaceable"><code>timeout</code></em> argument. If the blocked
client terminates, its thread does not die until the lock
request times out. This is a known bug.
</p></li><li><p><a id="function_inet-aton"></a>
<a id="id2853580" class="indexterm"></a>
<code class="literal">INET_ATON(<em class="replaceable"><code>expr</code></em>)</code>
</p><p>
Given the dotted-quad representation of a network address as
a string, returns an integer that represents the numeric
value of the address. Addresses may be 4- or 8-byte
addresses.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT INET_ATON('209.207.224.40');</code></strong>
-> 3520061480
</pre><p>
The generated number is always in network byte order. For
the example just shown, the number is calculated as
209×256<sup>3</sup> +
207×256<sup>2</sup> + 224×256 +
40.
</p><p>
<code class="literal">INET_ATON()</code> also understands short-form
IP addresses:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT INET_ATON('127.0.0.1'), INET_ATON('127.1');</code></strong>
-> 2130706433, 2130706433
</pre><p>
<span class="bold"><strong>Note</strong></span>: When storing values
generated by <code class="literal">INET_ATON()</code>, it is
recommended that you use an <code class="literal">INT UNSIGNED</code>
column. If you use a (signed) <code class="literal">INT</code> column,
values corresponding to IP addresses for which the first
octet is greater than 127 cannot be stored correctly. See
<a href="http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html" target="_top">Numeric Types</a>.
</p></li><li><p><a id="function_inet-ntoa"></a>
<a id="id2853714" class="indexterm"></a>
<code class="literal">INET_NTOA(<em class="replaceable"><code>expr</code></em>)</code>
</p><p>
Given a numeric network address (4 or 8 byte), returns the
dotted-quad representation of the address as a string.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT INET_NTOA(3520061480);</code></strong>
-> '209.207.224.40'
</pre></li><li><p><a id="function_is-free-lock"></a>
<a id="id2853781" class="indexterm"></a>
<code class="literal">IS_FREE_LOCK(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
Checks whether the lock named <em class="replaceable"><code>str</code></em>
is free to use (that is, not locked). Returns
<code class="literal">1</code> if the lock is free (no one is using
the lock), <code class="literal">0</code> if the lock is in use, and
<code class="literal">NULL</code> if an error occurs (such as an
incorrect argument).
</p></li><li><p><a id="function_is-used-lock"></a>
<a id="id2853851" class="indexterm"></a>
<code class="literal">IS_USED_LOCK(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
Checks whether the lock named <em class="replaceable"><code>str</code></em>
is in use (that is, locked). If so, it returns the
connection identifier of the client that holds the lock.
Otherwise, it returns <code class="literal">NULL</code>.
</p></li><li><p><a id="function_master-pos-wait"></a>
<a id="id2853912" class="indexterm"></a>
<code class="literal">MASTER_POS_WAIT(<em class="replaceable"><code>log_name</code></em>,<em class="replaceable"><code>log_pos</code></em>[,<em class="replaceable"><code>timeout</code></em>])</code>
</p><p>
This function is useful for control of master/slave
synchronization. It blocks until the slave has read and
applied all updates up to the specified position in the
master log. The return value is the number of log events the
slave had to wait for to advance to the specified position.
The function returns <code class="literal">NULL</code> if the slave
SQL thread is not started, the slave's master information is
not initialized, the arguments are incorrect, or an error
occurs. It returns <code class="literal">-1</code> if the timeout has
been exceeded. If the slave SQL thread stops while
<code class="literal">MASTER_POS_WAIT()</code> is waiting, the
function returns <code class="literal">NULL</code>. If the slave is
past the specified position, the function returns
immediately.
</p><p>
If a <em class="replaceable"><code>timeout</code></em> value is specified,
<code class="literal">MASTER_POS_WAIT()</code> stops waiting when
<em class="replaceable"><code>timeout</code></em> seconds have elapsed.
<em class="replaceable"><code>timeout</code></em> must be greater than 0; a
zero or negative <em class="replaceable"><code>timeout</code></em> means no
timeout.
</p></li><li><p><a id="function_name-const"></a>
<a id="id2854023" class="indexterm"></a>
<code class="literal">NAME_CONST(<em class="replaceable"><code>name</code></em>,<em class="replaceable"><code>value</code></em>)</code>
</p><p>
Returns the given value. When used to produce a result set
column, <code class="literal">NAME_CONST()</code> causes the column to
have the given name.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT NAME_CONST('myname', 14);</code></strong>
+--------+
| myname |
+--------+
| 14 |
+--------+
</pre><p>
This function was added in MySQL 5.0.12. It is for internal
use only. The server uses it when writing statements from
stored routines that contain references to local routine
variables, as described in
<a href="http://dev.mysql.com/doc/refman/5.0/en/stored-procedure-logging.html" target="_top">Binary Logging of Stored Routines and Triggers</a>, You might see
this function in the output from
<span><strong class="command">mysqlbinlog</strong></span>.
</p></li><li><p><a id="function_release-lock"></a>
<a id="id2854113" class="indexterm"></a>
<code class="literal">RELEASE_LOCK(<em class="replaceable"><code>str</code></em>)</code>
</p><p>
Releases the lock named by the string
<em class="replaceable"><code>str</code></em> that was obtained with
<code class="literal">GET_LOCK()</code>. Returns <code class="literal">1</code>
if the lock was released, <code class="literal">0</code> if the lock
was not established by this thread (in which case the lock
is not released), and <code class="literal">NULL</code> if the named
lock did not exist. The lock does not exist if it was never
obtained by a call to <code class="literal">GET_LOCK()</code> or if it
has previously been released.
</p><p>
The <code class="literal">DO</code> statement is convenient to use
with <code class="literal">RELEASE_LOCK()</code>. See
<a href="mysqlqb_statements.html#do" title="DO Syntax"><code class="literal">DO</code> Syntax</a>.
</p></li><li><p><a id="function_sleep"></a>
<a id="id2854209" class="indexterm"></a>
<code class="literal">SLEEP(<em class="replaceable"><code>duration</code></em>)</code>
</p><p>
Sleeps (pauses) for the number of seconds given by the
<em class="replaceable"><code>duration</code></em> argument, then returns
0. If <code class="literal">SLEEP()</code> is interrupted, it returns
1. The duration may have a fractional part given in
microseconds. This function was added in MySQL 5.0.12.
</p></li><li><p><a id="function_uuid"></a>
<a id="id2854272" class="indexterm"></a>
<code class="literal">UUID()</code>
</p><p>
Returns a Universal Unique Identifier (UUID) generated
according to “<span class="quote">DCE 1.1: Remote Procedure Call</span>”
(Appendix A) CAE (Common Applications Environment)
Specifications published by The Open Group in October 1997
(Document Number C706,
<a href="http://www.opengroup.org/public/pubs/catalog/c706.htm" target="_top">http://www.opengroup.org/public/pubs/catalog/c706.htm</a>).
</p><p>
A UUID is designed as a number that is globally unique in
space and time. Two calls to <code class="literal">UUID()</code> are
expected to generate two different values, even if these
calls are performed on two separate computers that are not
connected to each other.
</p><p>
A UUID is a 128-bit number represented by a string of five
hexadecimal numbers in
<code class="literal">aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee</code>
format:
</p><div class="itemizedlist"><ul type="circle"><li><p>
The first three numbers are generated from a timestamp.
</p></li><li><p>
The fourth number preserves temporal uniqueness in case
the timestamp value loses monotonicity (for example, due
to daylight saving time).
</p></li><li><p>
The fifth number is an IEEE 802 node number that
provides spatial uniqueness. A random number is
substituted if the latter is not available (for example,
because the host computer has no Ethernet card, or we do
not know how to find the hardware address of an
interface on your operating system). In this case,
spatial uniqueness cannot be guaranteed. Nevertheless, a
collision should have <span class="emphasis"><em>very</em></span> low
probability.
</p><p>
Currently, the MAC address of an interface is taken into
account only on FreeBSD and Linux. On other operating
systems, MySQL uses a randomly generated 48-bit number.
</p></li></ul></div><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT UUID();</code></strong>
-> '6ccd780c-baba-1026-9564-0040f4311e29'
</pre><p>
Note that <code class="literal">UUID()</code> does not yet work with
replication.
</p></li><li><p><a id="function_values"></a>
<a id="id2854425" class="indexterm"></a>
<code class="literal">VALUES(<em class="replaceable"><code>col_name</code></em>)</code>
</p><p>
In an <code class="literal">INSERT ... ON DUPLICATE KEY UPDATE</code>
statement, you can use the
<code class="literal">VALUES(<em class="replaceable"><code>col_name</code></em>)</code>
function in the <code class="literal">UPDATE</code> clause to refer to
column values from the <code class="literal">INSERT</code> portion of
the statement. In other words,
<code class="literal">VALUES(<em class="replaceable"><code>col_name</code></em>)</code>
in the <code class="literal">UPDATE</code> clause refers to the value
of <em class="replaceable"><code>col_name</code></em> that would be
inserted, had no duplicate-key conflict occurred. This
function is especially useful in multiple-row inserts. The
<code class="literal">VALUES()</code> function is meaningful only in
<code class="literal">INSERT ... ON DUPLICATE KEY UPDATE</code>
statements and returns <code class="literal">NULL</code> otherwise.
<a href="mysqlqb_statements.html#insert-on-duplicate" title="INSERT ... ON DUPLICATE KEY UPDATE Syntax"><code class="literal">INSERT ... ON DUPLICATE KEY UPDATE</code> Syntax</a>.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)</code></strong>
-> <strong class="userinput"><code>ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);</code></strong>
</pre></li></ul></div></div></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="group-by-functions-and-modifiers"></a>Functions and Modifiers for Use with <code class="literal">GROUP BY</code> Clauses</h2></div></div></div><div class="toc"><dl><dt><span class="section"><a href="mysqlqb_functions.html#group-by-functions"><code class="literal">GROUP BY</code> (Aggregate) Functions</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#group-by-modifiers"><code class="literal">GROUP BY</code> Modifiers</a></span></dt><dt><span class="section"><a href="mysqlqb_functions.html#group-by-hidden-fields"><code class="literal">GROUP BY</code> and <code class="literal">HAVING</code> with Hidden
Fields</a></span></dt></dl></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="group-by-functions"></a><code class="literal">GROUP BY</code> (Aggregate) Functions</h3></div></div></div><a id="id2854560" class="indexterm"></a><a id="id2854566" class="indexterm"></a><div class="informaltable"><table border="1"><colgroup><col /><col /></colgroup><thead><tr><th><span class="bold"><strong>Name</strong></span></th><th><span class="bold"><strong>Description</strong></span></th></tr></thead><tbody><tr><td><a href="mysqlqb_functions.html#function_avg"><code class="literal">AVG()</code></a></td><td>Return the average value of the argument</td></tr><tr><td><a href="mysqlqb_functions.html#function_count-distinct"><code class="literal">COUNT(DISTINCT)</code></a></td><td>Return the count of a number of different values</td></tr><tr><td><a href="mysqlqb_functions.html#function_group-concat"><code class="literal">GROUP_CONCAT()</code></a></td><td>Return a concatenated string</td></tr><tr><td><a href="mysqlqb_functions.html#function_min"><code class="literal">MAX()</code></a></td><td>Return the maximum value</td></tr><tr><td><a href="mysqlqb_functions.html#function_min"><code class="literal">MIN()</code></a></td><td>Return the minimum value</td></tr><tr><td><a href="mysqlqb_functions.html#function_std"><code class="literal">STD()</code>, <code class="literal">STDDEV()</code></a></td><td>Return the population standard deviation</td></tr><tr><td><a href="mysqlqb_functions.html#function_stddev-pop"><code class="literal">STDDEV_POP()</code></a></td><td>Return the population standard deviation</td></tr><tr><td><a href="mysqlqb_functions.html#function_stddev-samp"><code class="literal">STDDEV_SAMP()</code></a></td><td>Return the sample standard deviation</td></tr><tr><td><a href="mysqlqb_functions.html#function_sum"><code class="literal">SUM()</code></a></td><td>Return the sum</td></tr><tr><td><a href="mysqlqb_functions.html#function_var-pop"><code class="literal">VAR_POP()</code></a></td><td>Return the population standard variance</td></tr><tr><td><a href="mysqlqb_functions.html#function_var-samp"><code class="literal">VAR_SAMP()</code></a></td><td>Return the sample variance</td></tr><tr><td><a href="mysqlqb_functions.html#function_variance"><code class="literal">VARIANCE()</code></a></td><td>Return the population standard variance</td></tr></tbody></table></div><p>
This section describes group (aggregate) functions that operate
on sets of values. Unless otherwise stated, group functions
ignore <code class="literal">NULL</code> values.
</p><p>
If you use a group function in a statement containing no
<code class="literal">GROUP BY</code> clause, it is equivalent to grouping
on all rows.
</p><p>
For numeric arguments, the variance and standard deviation
functions return a <code class="literal">DOUBLE</code> value. The
<code class="literal">SUM()</code> and <code class="literal">AVG()</code> functions
return a <code class="literal">DECIMAL</code> value for exact-value
arguments (integer or <code class="literal">DECIMAL</code>), and a
<code class="literal">DOUBLE</code> value for approximate-value arguments
(<code class="literal">FLOAT</code> or <code class="literal">DOUBLE</code>). (Before
MySQL 5.0.3, <code class="literal">SUM()</code> and
<code class="literal">AVG()</code> return <code class="literal">DOUBLE</code> for
all numeric arguments.)
</p><p>
The <code class="literal">SUM()</code> and <code class="literal">AVG()</code>
aggregate functions do not work with temporal values. (They
convert the values to numbers, losing everything after the first
non-numeric character.) To work around this problem, you can
convert to numeric units, perform the aggregate operation, and
convert back to a temporal value. Examples:
</p><pre class="programlisting">SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(<em class="replaceable"><code>time_col</code></em>))) FROM <em class="replaceable"><code>tbl_name</code></em>;
SELECT FROM_DAYS(SUM(TO_DAYS(<em class="replaceable"><code>date_col</code></em>))) FROM <em class="replaceable"><code>tbl_name</code></em>;
</pre><div class="itemizedlist"><ul type="disc"><li><p><a id="function_avg"></a>
<a id="id2854888" class="indexterm"></a>
<a id="id2854895" class="indexterm"></a>
<a id="id2854902" class="indexterm"></a>
<code class="literal">AVG([DISTINCT]
<em class="replaceable"><code>expr</code></em>)</code>
</p><p>
Returns the average value of
<code class="literal"><em class="replaceable"><code>expr</code></em></code>. The
<code class="literal">DISTINCT</code> option can be used as of MySQL
5.0.3 to return the average of the distinct values of
<em class="replaceable"><code>expr</code></em>.
</p><p>
<code class="literal">AVG()</code> returns <code class="literal">NULL</code> if
there were no matching rows.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT student_name, AVG(test_score)</code></strong>
-> <strong class="userinput"><code>FROM student</code></strong>
-> <strong class="userinput"><code>GROUP BY student_name;</code></strong>
</pre></li><li><p><a id="function_bit-and"></a>
<a id="id2855003" class="indexterm"></a>
<code class="literal">BIT_AND(<em class="replaceable"><code>expr</code></em>)</code>
</p><p>
Returns the bitwise <code class="literal">AND</code> of all bits in
<em class="replaceable"><code>expr</code></em>. The calculation is
performed with 64-bit (<code class="literal">BIGINT</code>) precision.
</p><p>
This function returns
<code class="literal">18446744073709551615</code> if there were no
matching rows. (This is the value of an unsigned
<code class="literal">BIGINT</code> value with all bits set to 1.)
</p></li><li><p><a id="function_bit-or"></a>
<a id="id2855081" class="indexterm"></a>
<code class="literal">BIT_OR(<em class="replaceable"><code>expr</code></em>)</code>
</p><p>
Returns the bitwise <code class="literal">OR</code> of all bits in
<em class="replaceable"><code>expr</code></em>. The calculation is
performed with 64-bit (<code class="literal">BIGINT</code>) precision.
</p><p>
This function returns <code class="literal">0</code> if there were no
matching rows.
</p></li><li><p><a id="function_bit-xor"></a>
<a id="id2855152" class="indexterm"></a>
<code class="literal">BIT_XOR(<em class="replaceable"><code>expr</code></em>)</code>
</p><p>
Returns the bitwise <code class="literal">XOR</code> of all bits in
<em class="replaceable"><code>expr</code></em>. The calculation is
performed with 64-bit (<code class="literal">BIGINT</code>) precision.
</p><p>
This function returns <code class="literal">0</code> if there were no
matching rows.
</p></li><li><p><a id="function_count"></a>
<a id="id2855224" class="indexterm"></a>
<code class="literal">COUNT(<em class="replaceable"><code>expr</code></em>)</code>
</p><p>
Returns a count of the number of non-<code class="literal">NULL</code>
values in the rows retrieved by a <code class="literal">SELECT</code>
statement. The result is a <code class="literal">BIGINT</code> value.
</p><p>
<code class="literal">COUNT()</code> returns <code class="literal">0</code> if
there were no matching rows.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT student.student_name,COUNT(*)</code></strong>
-> <strong class="userinput"><code>FROM student,course</code></strong>
-> <strong class="userinput"><code>WHERE student.student_id=course.student_id</code></strong>
-> <strong class="userinput"><code>GROUP BY student_name;</code></strong>
</pre><p>
<code class="literal">COUNT(*)</code> is somewhat different in that it
returns a count of the number of rows retrieved, whether or
not they contain <code class="literal">NULL</code> values.
</p><p>
<code class="literal">COUNT(*)</code> is optimized to return very
quickly if the <code class="literal">SELECT</code> retrieves from one
table, no other columns are retrieved, and there is no
<code class="literal">WHERE</code> clause. For example:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT COUNT(*) FROM student;</code></strong>
</pre><p>
This optimization applies only to <code class="literal">MyISAM</code>
tables only, because an exact row count is stored for this
storage engine and can be accessed very quickly. For
transactional storage engines such as
<code class="literal">InnoDB</code> and <code class="literal">BDB</code>,
storing an exact row count is more problematic because
multiple transactions may be occurring, each of which may
affect the count.
</p></li><li><p><a id="function_count-distinct"></a>
<a id="id2855390" class="indexterm"></a>
<a id="id2855396" class="indexterm"></a>
<code class="literal">COUNT(DISTINCT
<em class="replaceable"><code>expr</code></em>,[<em class="replaceable"><code>expr</code></em>...])</code>
</p><p>
Returns a count of the number of different
non-<code class="literal">NULL</code> values.
</p><p>
<code class="literal">COUNT(DISTINCT)</code> returns
<code class="literal">0</code> if there were no matching rows.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT COUNT(DISTINCT results) FROM student;</code></strong>
</pre><p>
In MySQL, you can obtain the number of distinct expression
combinations that do not contain <code class="literal">NULL</code> by
giving a list of expressions. In standard SQL, you would
have to do a concatenation of all expressions inside
<code class="literal">COUNT(DISTINCT ...)</code>.
</p></li><li><p><a id="function_group-concat"></a>
<a id="id2855506" class="indexterm"></a>
<code class="literal">GROUP_CONCAT(<em class="replaceable"><code>expr</code></em>)</code>
</p><p>
This function returns a string result with the concatenated
non-<code class="literal">NULL</code> values from a group. It returns
<code class="literal">NULL</code> if there are no
non-<code class="literal">NULL</code> values. The full syntax is as
follows:
</p><pre class="programlisting">GROUP_CONCAT([DISTINCT] <em class="replaceable"><code>expr</code></em> [,<em class="replaceable"><code>expr</code></em> ...]
[ORDER BY {<em class="replaceable"><code>unsigned_integer</code></em> | <em class="replaceable"><code>col_name</code></em> | <em class="replaceable"><code>expr</code></em>}
[ASC | DESC] [,<em class="replaceable"><code>col_name</code></em> ...]]
[SEPARATOR <em class="replaceable"><code>str_val</code></em>])
</pre><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT student_name,</code></strong>
-> <strong class="userinput"><code>GROUP_CONCAT(test_score)</code></strong>
-> <strong class="userinput"><code>FROM student</code></strong>
-> <strong class="userinput"><code>GROUP BY student_name;</code></strong>
</pre><p>
Or:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT student_name,</code></strong>
-> <strong class="userinput"><code>GROUP_CONCAT(DISTINCT test_score</code></strong>
-> <strong class="userinput"><code>ORDER BY test_score DESC SEPARATOR ' ')</code></strong>
-> <strong class="userinput"><code>FROM student</code></strong>
-> <strong class="userinput"><code>GROUP BY student_name;</code></strong>
</pre><p>
In MySQL, you can get the concatenated values of expression
combinations. You can eliminate duplicate values by using
<code class="literal">DISTINCT</code>. If you want to sort values in
the result, you should use <code class="literal">ORDER BY</code>
clause. To sort in reverse order, add the
<code class="literal">DESC</code> (descending) keyword to the name of
the column you are sorting by in the <code class="literal">ORDER
BY</code> clause. The default is ascending order; this
may be specified explicitly using the <code class="literal">ASC</code>
keyword. <code class="literal">SEPARATOR</code> is followed by the
string value that should be inserted between values of
result. The default is a comma
(‘<code class="literal">,</code>’). You can eliminate the
separator altogether by specifying <code class="literal">SEPARATOR
''</code>.
</p><p>
You can set a maximum allowed length with the
<code class="literal">group_concat_max_len</code> system variable.
(The default value is 1024.) The syntax to do this at
runtime is as follows, where <em class="replaceable"><code>val</code></em>
is an unsigned integer:
</p><pre class="programlisting">SET [SESSION | GLOBAL] group_concat_max_len = <em class="replaceable"><code>val</code></em>;
</pre><p>
If a maximum length has been set, the result is truncated to
this maximum length.
</p><p>
Beginning with MySQL 5.0.19, the type returned by
<code class="literal">GROUP_CONCAT()</code> is always
<code class="literal">VARCHAR</code> unless
<code class="literal">group_concat_max_len</code> is greater than 512,
in which case, it returns a <code class="literal">BLOB</code>.
(Previously, it returned a <code class="literal">BLOB</code> with
<code class="literal">group_concat_max_len</code> greater than 512
only if the query included an <code class="literal">ORDER BY</code>
clause.)
</p><p>
See also <code class="literal">CONCAT()</code> and
<code class="literal">CONCAT_WS()</code>:
<a href="mysqlqb_functions.html#string-functions" title="String Functions">String Functions</a>.
</p></li><li><p><a id="function_min"></a>
<a id="id2855794" class="indexterm"></a>
<a id="id2855801" class="indexterm"></a>
<a id="id2855808" class="indexterm"></a>
<a id="id2855818" class="indexterm"></a>
<a id="id2855824" class="indexterm"></a>
<a id="id2855831" class="indexterm"></a>
<code class="literal">MIN([DISTINCT]
<em class="replaceable"><code>expr</code></em>)</code>,
<code class="literal">MAX([DISTINCT]
<em class="replaceable"><code>expr</code></em>)</code>
</p><p>
Returns the minimum or maximum value of
<em class="replaceable"><code>expr</code></em>. <code class="literal">MIN()</code>
and <code class="literal">MAX()</code> may take a string argument; in
such cases they return the minimum or maximum string value.
See <a href="http://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html" target="_top">How MySQL Uses Indexes</a>. The
<code class="literal">DISTINCT</code> keyword can be used to find the
minimum or maximum of the distinct values of
<em class="replaceable"><code>expr</code></em>, however, this produces the
same result as omitting <code class="literal">DISTINCT</code>.
</p><p>
<code class="literal">MIN()</code> and <code class="literal">MAX()</code> return
<code class="literal">NULL</code> if there were no matching rows.
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT student_name, MIN(test_score), MAX(test_score)</code></strong>
-> <strong class="userinput"><code>FROM student</code></strong>
-> <strong class="userinput"><code>GROUP BY student_name;</code></strong>
</pre><p>
For <code class="literal">MIN()</code>, <code class="literal">MAX()</code>, and
other aggregate functions, MySQL currently compares
<code class="literal">ENUM</code> and <code class="literal">SET</code> columns
by their string value rather than by the string's relative
position in the set. This differs from how <code class="literal">ORDER
BY</code> compares them. This is expected to be rectified
in a future MySQL release.
</p></li><li><p><a id="function_std"></a>
<a id="id2855997" class="indexterm"></a>
<a id="id2856004" class="indexterm"></a>
<a id="id2856010" class="indexterm"></a>
<a id="id2856017" class="indexterm"></a>
<code class="literal">STD(<em class="replaceable"><code>expr</code></em>)</code>
<code class="literal">STDDEV(<em class="replaceable"><code>expr</code></em>)</code>
</p><p>
Returns the population standard deviation of
<em class="replaceable"><code>expr</code></em>. This is an extension to
standard SQL. The <code class="literal">STDDEV()</code> form of this
function is provided for compatibility with Oracle. As of
MySQL 5.0.3, the standard SQL function
<code class="literal">STDDEV_POP()</code> can be used instead.
</p><p>
These functions return <code class="literal">NULL</code> if there were
no matching rows.
</p></li><li><p><a id="function_stddev-pop"></a>
<a id="id2856103" class="indexterm"></a>
<code class="literal">STDDEV_POP(<em class="replaceable"><code>expr</code></em>)</code>
</p><p>
Returns the population standard deviation of
<em class="replaceable"><code>expr</code></em> (the square root of
<code class="literal">VAR_POP()</code>). This function was added in
MySQL 5.0.3. Before 5.0.3, you can use
<code class="literal">STD()</code> or <code class="literal">STDDEV()</code>,
which are equivalent but not standard SQL.
</p><p>
<code class="literal">STDDEV_POP()</code> returns
<code class="literal">NULL</code> if there were no matching rows.
</p></li><li><p><a id="function_stddev-samp"></a>
<a id="id2856185" class="indexterm"></a>
<code class="literal">STDDEV_SAMP(<em class="replaceable"><code>expr</code></em>)</code>
</p><p>
Returns the sample standard deviation of
<em class="replaceable"><code>expr</code></em> (the square root of
<code class="literal">VAR_SAMP()</code>. This function was added in
MySQL 5.0.3.
</p><p>
<code class="literal">STDDEV_SAMP()</code> returns
<code class="literal">NULL</code> if there were no matching rows.
</p></li><li><p><a id="function_sum"></a>
<a id="id2856263" class="indexterm"></a>
<a id="id2856270" class="indexterm"></a>
<a id="id2856277" class="indexterm"></a>
<code class="literal">SUM([DISTINCT]
<em class="replaceable"><code>expr</code></em>)</code>
</p><p>
Returns the sum of <em class="replaceable"><code>expr</code></em>. If the
return set has no rows, <code class="literal">SUM()</code> returns
<code class="literal">NULL</code>. The <code class="literal">DISTINCT</code>
keyword can be used in MySQL 5.0 to sum only
the distinct values of <em class="replaceable"><code>expr</code></em>.
</p><p>
<code class="literal">SUM()</code> returns <code class="literal">NULL</code> if
there were no matching rows.
</p></li><li><p><a id="function_var-pop"></a>
<a id="id2856364" class="indexterm"></a>
<code class="literal">VAR_POP(<em class="replaceable"><code>expr</code></em>)</code>
</p><p>
Returns the population standard variance of
<em class="replaceable"><code>expr</code></em>. It considers rows as the
whole population, not as a sample, so it has the number of
rows as the denominator. This function was added in MySQL
5.0.3. Before 5.0.3, you can use
<code class="literal">VARIANCE()</code>, which is equivalent but is
not standard SQL.
</p><p>
<code class="literal">VAR_POP()</code> returns <code class="literal">NULL</code>
if there were no matching rows.
</p></li><li><p><a id="function_var-samp"></a>
<a id="id2856440" class="indexterm"></a>
<code class="literal">VAR_SAMP(<em class="replaceable"><code>expr</code></em>)</code>
</p><p>
Returns the sample variance of
<em class="replaceable"><code>expr</code></em>. That is, the denominator is
the number of rows minus one. This function was added in
MySQL 5.0.3.
</p><p>
<code class="literal">VAR_SAMP()</code> returns
<code class="literal">NULL</code> if there were no matching rows.
</p></li><li><p><a id="function_variance"></a>
<a id="id2856509" class="indexterm"></a>
<code class="literal">VARIANCE(<em class="replaceable"><code>expr</code></em>)</code>
</p><p>
Returns the population standard variance of
<em class="replaceable"><code>expr</code></em>. This is an extension to
standard SQL. As of MySQL 5.0.3, the standard SQL function
<code class="literal">VAR_POP()</code> can be used instead.
</p><p>
<code class="literal">VARIANCE()</code> returns
<code class="literal">NULL</code> if there were no matching rows.
</p></li></ul></div></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="group-by-modifiers"></a><code class="literal">GROUP BY</code> Modifiers</h3></div></div></div><a id="id2856577" class="indexterm"></a><a id="id2856584" class="indexterm"></a><p>
The <code class="literal">GROUP BY</code> clause allows a <code class="literal">WITH
ROLLUP</code> modifier that causes extra rows to be added to
the summary output. These rows represent higher-level (or
super-aggregate) summary operations. <code class="literal">ROLLUP</code>
thus allows you to answer questions at multiple levels of
analysis with a single query. It can be used, for example, to
provide support for OLAP (Online Analytical Processing)
operations.
</p><p>
Suppose that a table named <code class="literal">sales</code> has
<code class="literal">year</code>, <code class="literal">country</code>,
<code class="literal">product</code>, and <code class="literal">profit</code>
columns for recording sales profitability:
</p><pre class="programlisting">CREATE TABLE sales
(
year INT NOT NULL,
country VARCHAR(20) NOT NULL,
product VARCHAR(32) NOT NULL,
profit INT
);
</pre><p>
The table's contents can be summarized per year with a simple
<code class="literal">GROUP BY</code> like this:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT year, SUM(profit) FROM sales GROUP BY year;</code></strong>
+------+-------------+
| year | SUM(profit) |
+------+-------------+
| 2000 | 4525 |
| 2001 | 3010 |
+------+-------------+
</pre><p>
This output shows the total profit for each year, but if you
also want to determine the total profit summed over all years,
you must add up the individual values yourself or run an
additional query.
</p><p>
Or you can use <code class="literal">ROLLUP</code>, which provides both
levels of analysis with a single query. Adding a <code class="literal">WITH
ROLLUP</code> modifier to the <code class="literal">GROUP BY</code>
clause causes the query to produce another row that shows the
grand total over all year values:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT year, SUM(profit) FROM sales GROUP BY year WITH ROLLUP;</code></strong>
+------+-------------+
| year | SUM(profit) |
+------+-------------+
| 2000 | 4525 |
| 2001 | 3010 |
| NULL | 7535 |
+------+-------------+
</pre><p>
The grand total super-aggregate line is identified by the value
<code class="literal">NULL</code> in the <code class="literal">year</code> column.
</p><p>
<code class="literal">ROLLUP</code> has a more complex effect when there
are multiple <code class="literal">GROUP BY</code> columns. In this case,
each time there is a “<span class="quote">break</span>” (change in value) in
any but the last grouping column, the query produces an extra
super-aggregate summary row.
</p><p>
For example, without <code class="literal">ROLLUP</code>, a summary on the
<code class="literal">sales</code> table based on <code class="literal">year</code>,
<code class="literal">country</code>, and <code class="literal">product</code> might
look like this:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT year, country, product, SUM(profit)</code></strong>
-> <strong class="userinput"><code>FROM sales</code></strong>
-> <strong class="userinput"><code>GROUP BY year, country, product;</code></strong>
+------+---------+------------+-------------+
| year | country | product | SUM(profit) |
+------+---------+------------+-------------+
| 2000 | Finland | Computer | 1500 |
| 2000 | Finland | Phone | 100 |
| 2000 | India | Calculator | 150 |
| 2000 | India | Computer | 1200 |
| 2000 | USA | Calculator | 75 |
| 2000 | USA | Computer | 1500 |
| 2001 | Finland | Phone | 10 |
| 2001 | USA | Calculator | 50 |
| 2001 | USA | Computer | 2700 |
| 2001 | USA | TV | 250 |
+------+---------+------------+-------------+
</pre><p>
The output indicates summary values only at the
year/country/product level of analysis. When
<code class="literal">ROLLUP</code> is added, the query produces several
extra rows:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT year, country, product, SUM(profit)</code></strong>
-> <strong class="userinput"><code>FROM sales</code></strong>
-> <strong class="userinput"><code>GROUP BY year, country, product WITH ROLLUP;</code></strong>
+------+---------+------------+-------------+
| year | country | product | SUM(profit) |
+------+---------+------------+-------------+
| 2000 | Finland | Computer | 1500 |
| 2000 | Finland | Phone | 100 |
| 2000 | Finland | NULL | 1600 |
| 2000 | India | Calculator | 150 |
| 2000 | India | Computer | 1200 |
| 2000 | India | NULL | 1350 |
| 2000 | USA | Calculator | 75 |
| 2000 | USA | Computer | 1500 |
| 2000 | USA | NULL | 1575 |
| 2000 | NULL | NULL | 4525 |
| 2001 | Finland | Phone | 10 |
| 2001 | Finland | NULL | 10 |
| 2001 | USA | Calculator | 50 |
| 2001 | USA | Computer | 2700 |
| 2001 | USA | TV | 250 |
| 2001 | USA | NULL | 3000 |
| 2001 | NULL | NULL | 3010 |
| NULL | NULL | NULL | 7535 |
+------+---------+------------+-------------+
</pre><p>
For this query, adding <code class="literal">ROLLUP</code> causes the
output to include summary information at four levels of
analysis, not just one. Here's how to interpret the
<code class="literal">ROLLUP</code> output:
</p><div class="itemizedlist"><ul type="disc"><li><p>
Following each set of product rows for a given year and
country, an extra summary row is produced showing the total
for all products. These rows have the
<code class="literal">product</code> column set to
<code class="literal">NULL</code>.
</p></li><li><p>
Following each set of rows for a given year, an extra
summary row is produced showing the total for all countries
and products. These rows have the <code class="literal">country</code>
and <code class="literal">products</code> columns set to
<code class="literal">NULL</code>.
</p></li><li><p>
Finally, following all other rows, an extra summary row is
produced showing the grand total for all years, countries,
and products. This row has the <code class="literal">year</code>,
<code class="literal">country</code>, and <code class="literal">products</code>
columns set to <code class="literal">NULL</code>.
</p></li></ul></div><p>
<span class="bold"><strong>Other Considerations When using
<code class="literal">ROLLUP</code></strong></span>
</p><p>
The following items list some behaviors specific to the MySQL
implementation of <code class="literal">ROLLUP</code>:
</p><p>
When you use <code class="literal">ROLLUP</code>, you cannot also use an
<code class="literal">ORDER BY</code> clause to sort the results. In other
words, <code class="literal">ROLLUP</code> and <code class="literal">ORDER BY</code>
are mutually exclusive. However, you still have some control
over sort order. <code class="literal">GROUP BY</code> in MySQL sorts
results, and you can use explicit <code class="literal">ASC</code> and
<code class="literal">DESC</code> keywords with columns named in the
<code class="literal">GROUP BY</code> list to specify sort order for
individual columns. (The higher-level summary rows added by
<code class="literal">ROLLUP</code> still appear after the rows from which
they are calculated, regardless of the sort order.)
</p><p>
<code class="literal">LIMIT</code> can be used to restrict the number of
rows returned to the client. <code class="literal">LIMIT</code> is applied
after <code class="literal">ROLLUP</code>, so the limit applies against
the extra rows added by <code class="literal">ROLLUP</code>. For example:
</p><pre class="programlisting">mysql> <strong class="userinput"><code>SELECT year, country, product, SUM(profit)</code></strong>
-> <strong class="userinput"><code>FROM sales</code></strong>
-> <strong class="userinput"><code>GROUP BY year, country, product WITH ROLLUP</code></strong>
-> <strong class="userinput"><code>LIMIT 5;</code></strong>
+------+---------+------------+-------------+
| year | country | product | SUM(profit) |
+------+---------+------------+-------------+
| 2000 | Finland | Computer | 1500 |
| 2000 | Finland | Phone | 100 |
| 2000 | Finland | NULL | 1600 |
| 2000 | India | Calculator | 150 |
| 2000 | India | Computer | 1200 |
+------+---------+------------+-------------+
</pre><p>
Using <code class="literal">LIMIT</code> with <code class="literal">ROLLUP</code>
may produce results that are more difficult to interpret,
because you have less context for understanding the
super-aggregate rows.
</p><p>
The <code class="literal">NULL</code> indicators in each super-aggregate
row are produced when the row is sent to the client. The server
looks at the columns named in the <code class="literal">GROUP BY</code>
clause following the leftmost one that has changed value. For
any column in the result set with a name that is a lexical match
to any of those names, its value is set to
<code class="literal">NULL</code>. (If you specify grouping columns by
column number, the server identifies which columns to set to
<code class="literal">NULL</code> by number.)
</p><p>
Because the <code class="literal">NULL</code> values in the
super-aggregate rows are placed into the result set at such a
late stage in query processing, you cannot test them as
<code class="literal">NULL</code> values within the query itself. For
example, you cannot add <code class="literal">HAVING product IS
NULL</code> to the query to eliminate from the output all but
the super-aggregate rows.
</p><p>
On the other hand, the <code class="literal">NULL</code> values do appear
as <code class="literal">NULL</code> on the client side and can be tested
as such using any MySQL client programming interface.
</p></div><div class="section" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="group-by-hidden-fields"></a><code class="literal">GROUP BY</code> and <code class="literal">HAVING</code> with Hidden
Fields</h3></div></div></div><a id="id2857261" class="indexterm"></a><a id="id2857272" class="indexterm"></a><a id="id2857283" class="indexterm"></a><p>
MySQL extends the use of <code class="literal">GROUP BY</code> so that you
can use non-aggregated columns or calculations in the
<code class="literal">SELECT</code> list that do not appear in the
<code class="literal">GROUP BY</code> clause. You can use this feature to
get better performance by avoiding unnecessary column sorting
and grouping. For example, you do not need to group on
<code class="literal">customer.name</code> in the following query:
</p><pre class="programlisting">SELECT order.custid, customer.name, MAX(payments)
FROM order,customer
WHERE order.custid = customer.custid
GROUP BY order.custid;
</pre><p>
In standard SQL, you would have to add
<code class="literal">customer.name</code> to the <code class="literal">GROUP
BY</code> clause. In MySQL, the name is redundant.
</p><p>
Do <span class="emphasis"><em>not</em></span> use this feature if the columns you
omit from the <code class="literal">GROUP BY</code> part are not constant
in the group. The server is free to return any value from the
group, so the results are indeterminate unless all values are
the same.
</p><p>
A similar MySQL extension applies to the
<code class="literal">HAVING</code> clause. The SQL standard does not
allow the <code class="literal">HAVING</code> clause to name any column
that is not found in the <code class="literal">GROUP BY</code> clause if
it is not enclosed in an aggregate function. MySQL allows the
use of such columns to simplify calculations. This extension
assumes that the non-grouped columns will have the same
group-wise values. Otherwise, the result is indeterminate.
</p><p>
If the <code class="literal">ONLY_FULL_GROUP_BY</code> SQL mode is
enabled, the MySQL extension to <code class="literal">GROUP BY</code> does
not apply. That is, columns not named in the <code class="literal">GROUP
BY</code> clause cannot be used in the
<code class="literal">SELECT</code> list or <code class="literal">HAVING</code>
clause if not used in an aggregate function.
</p><p>
The select list extension also applies to <code class="literal">ORDER
BY</code>. That is, you can use non-aggregated columns or
calculations in the <code class="literal">ORDER BY</code> clause that do
not appear in the <code class="literal">GROUP BY</code> clause. This
extension does not apply if the
<code class="literal">ONLY_FULL_GROUP_BY</code> SQL mode is enabled.
</p><p>
In some cases, you can use <code class="literal">MIN()</code> and
<code class="literal">MAX()</code> to obtain a specific column value even
if it isn't unique. The following gives the value of
<code class="literal">column</code> from the row containing the smallest
value in the <code class="literal">sort</code> column:
</p><pre class="programlisting">SUBSTR(MIN(CONCAT(RPAD(sort,6,' '),column)),7)
</pre><p>
See <a href="http://dev.mysql.com/doc/refman/5.0/en/example-maximum-column-group-row.html" target="_top">The Rows Holding the Group-wise Maximum of a Certain Field</a>.
</p><a id="id2857459" class="indexterm"></a><a id="id2857470" class="indexterm"></a><a id="id2857484" class="indexterm"></a><a id="id2857491" class="indexterm"></a><p>
Note that if you are trying to follow standard SQL, you can't
use expressions in <code class="literal">GROUP BY</code> clauses. You can
work around this limitation by using an alias for the
expression:
</p><pre class="programlisting">SELECT id,FLOOR(value/100) AS val
FROM <em class="replaceable"><code>tbl_name</code></em>
GROUP BY id, val;
</pre><p>
MySQL does allow expressions in <code class="literal">GROUP BY</code>
clauses. For example:
</p><pre class="programlisting">SELECT id,FLOOR(value/100)
FROM <em class="replaceable"><code>tbl_name</code></em>
GROUP BY id, FLOOR(value/100);
</pre></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="index.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="mysqlqb_statements.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Query Browser Help </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> SQL Statement Syntax</td></tr></table></div></body></html>
|