Equivalent mysql expression before '0000-00-00 00:00:00'

I would like to know if a shorter expression exists than this:

'0000-00-00 00:00:00'


SELECT id FROM orders WHERE posted='0000-00-00 00:00:00' ORDER BY id DESC LIMIT 1;

I mean something like:

SELECT id FROM orders WHERE posted=null ORDER BY id DESC LIMIT 1;

Hope this is the circuit you wanted to see:

array(19) {
  ["TABLE_CATALOG"]=>
  NULL
  ["TABLE_SCHEMA"]=>
  string(13) "teleprintfejl"
  ["TABLE_NAME"]=>
  string(13) "megrendelesek"
  ["COLUMN_NAME"]=>
  string(7) "posted"
  ["ORDINAL_POSITION"]=>
  string(2) "20"
  ["COLUMN_DEFAULT"]=>
  NULL
  ["IS_NULLABLE"]=>
  string(2) "NO"
  ["DATA_TYPE"]=>
  string(8) "datetime"
  ["CHARACTER_MAXIMUM_LENGTH"]=>
  NULL
  ["CHARACTER_OCTET_LENGTH"]=>
  NULL
  ["NUMERIC_PRECISION"]=>
  NULL
  ["NUMERIC_SCALE"]=>
  NULL
  ["CHARACTER_SET_NAME"]=>
  NULL
  ["COLLATION_NAME"]=>
  NULL
  ["COLUMN_TYPE"]=>
  string(8) "datetime"
  ["COLUMN_KEY"]=>
  string(0) ""
  ["EXTRA"]=>
  string(0) ""
  ["PRIVILEGES"]=>
  string(31) "select,insert,update,references"
  ["COLUMN_COMMENT"]=>
  string(0) ""
}
+5
source share
3 answers

I guess that postedis DATETIME. You can use 0.

posted = '0000-00-00 00:00:00'

coincides with

posted = 0
+7
source

as for your structure, when you create it, you can use null by default, and when you make a choice, you will use:

SELECT id FROM orders WHERE posted IS null ORDER BY id DESC LIMIT 1;

EDIT:

mysql> create table tiempo (fecha datetime);
Query OK, 0 rows affected (0.04 sec)

mysql> insert into tiempo values ();
Query OK, 1 row affected (0.00 sec)

mysql> select * from tiempo;
+-------+
| fecha |
+-------+
| NULL  |
+-------+
1 row in set (0.00 sec)

mysql> insert into tiempo values ('2000-02-02 02:02:02');
Query OK, 1 row affected (0.00 sec)

mysql> select * from tiempo;
+---------------------+
| fecha               |
+---------------------+
| NULL                |
| 2000-02-02 02:02:02 |
+---------------------+
2 rows in set (0.00 sec)

mysql> select * from tiempo where fecha is not null;
+---------------------+
| fecha               |
+---------------------+
| 2000-02-02 02:02:02 |
+---------------------+
1 row in set (0.00 sec)
0
source
SELECT max(id) as id FROM orders WHERE posted=0;
0
source

All Articles