Mysql: come eseguire una query su una colonna il cui tipo è bit?
Domanda
Ciao, sto usando ibernazione e Mysql. Ho una classe con un attributo booleano chiamato "attivo".
La tabella del database generata ha il tipo di dati BIT. Fin qui tutto bene. Voglio interrogare questo valore ma non so come farlo. Ho provato
SELECT * from table where active = 1
non funziona, né i seguenti
SELECT * from table where active = true
Non ho trovato nulla né nel manuale di riferimento né su Stackoveflow.
Qualche suggerimento?
Grazie in anticipo!
Soluzione
SELECT * FROM table WHERE active = (1)
Altri suggerimenti
Secondo questa pagina , BIT è sinonimo di TINYINT (1) per le versioni precedenti alla 5.0.3 .
Hai provato questi?
SELECT * from table where active = (1)
SELECT * from table where active = 'true'
SELECT * from table where active = b'1'
Questo post di blog suggerisce di evitare del tutto il tipo di dati BIT.
Per specificare i valori di bit, è possibile utilizzare la notazione b'value '.
Hai provato a trasmetterlo a un numero intero per il confronto
SELECT * from table where cast(active as unsigned) = 1
Uso MS SQL il più delle volte, quindi perdonami se questo non funziona perché non riesco a testarlo.
In realtà MySQL ha letterali bit incorporati:
select*from table where active = 0b1
Bene, sia per i confronti che per gli aggiornamenti, 0 e 1 funzionano per me:
Ecco un campo di tipo bit (1), una riga, il campo è attualmente falso:
mysql> select isfeatured from nodes where isfeatured = 1;
Empty set (0.00 sec)
mysql> select isfeatured from nodes where isfeatured = 0;
+------------+
| isfeatured |
+------------+
| |
+------------+
1 row in set (0.00 sec)
L'aggiornamento cambia da 0 a 1 in isfeatured, che è di tipo bit (1) ...
mysql> update nodes set isfeatured=1 where isfeatured = 0;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
Una riga è cambiata ... Riprovare:
mysql> update nodes set isfeatured=1 where isfeatured = 0;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0
Nessuna riga modificata come previsto.
Stesse query di selezione di prima:
mysql> select isfeatured from nodes where isfeatured = 1;
+------------+
| isfeatured |
+------------+
| |
+------------+
1 row in set (0.00 sec)
mysql> select isfeatured from nodes where isfeatured = 0;
Empty set (0.01 sec)
Vedi, funziona.
Sto usando:
mysql Ver 14.14 Distrib 5.5.31, per debian-linux-gnu (x86_64) usando readline 6.2
e
/ usr / sbin / mysqld Ver 5.5.31-0 + wheezy1 per debian-linux-gnu su x86_64 ((Debian))