Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

การดำเนินการ NAND และ NOR ใน MySQL

MySQL ไม่มีฟังก์ชัน NAND/NOR ตรงๆ แต่สามารถคำนวณได้จาก NOT, AND, OR

ตรรกะพื้นฐาน

NAND = NOT( yourColumnName1 AND yourColumnName2 )
NOR  = NOT( yourColumnName1 OR yourColumnName2 )

สร้างตารางตัวอย่าง

mysql> create table DemoTable ( 
    Value1 boolean , 
    Value2 boolean ); 
Query OK, 0 rows affected (0.72 sec)

เพิ่มข้อมูลตัวอย่าง (Truth Table)

mysql> insert into DemoTable values(true,true); Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values(false,false); Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values(false,true); Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values(true,false); Query OK, 1 row affected (0.12 sec)

แสดงข้อมูลทั้งหมด

mysql> select *from DemoTable;
+--------+--------+
| Value1 | Value2 |
+--------+--------+
|      1 |      1 |
|      0 |      0 |
|      0 |      1 |
|      1 |      0 |
+--------+--------+
4 rows in set (0.00 sec)

คิวรี่คำนวณ NAND และ NOR

mysql> select Value1,Value2, NOT(Value1 AND Value2) AS NAND_Result,NOT(Value1 OR Value2) AS NOR_Result from DemoTable;

ผลลัพธ์:

+--------+--------+-------------+------------+
| Value1 | Value2 | NAND_Result | NOR_Result |
+--------+--------+-------------+------------+
|      1 |      1 |           0 |          0 |
|      0 |      0 |           1 |          1 |
|      0 |      1 |           1 |          0 |
|      1 |      0 |           1 |          0 |
+--------+--------+-------------+------------+
4 rows in set (0.00 sec)

ตารางความจริง (Truth Table)

ABANDNANDORNOR
111010
100110
010110
000101

การประยุกต์ใช้

  • NAND/NOR ใช้ใน Digital Logic Design
  • ใน SQL ใช้ได้สำหรับเงื่อนไขซับซ้อน เช่น "ไม่ใช่ทั้ง A และ B พร้อมกัน"
  • MySQL แปลง BOOLEAN เป็น TINYINT(1) โดย 1 = TRUE, 0 = FALSE