Hi, I need your appreciated help
In the table do_table
stored an a database MySql version 8.0.17
I have these rows
+----+-------------------------+-----+
| ts | tt | tID |
+----+-------------------------+-----+
| 1 | t_contents_11111_2_2021 | 1 |
| 0 | t_contents_2222_2_2021 | 2 |
| 1 | t_contents_3333_2_2021 | 3 |
+----+-------------------------+-----+
Using SQL
I need:
- Checking if table exists in the
database
because it may not have been created yet; - And if
ts
field is equal tozero
the return of@t
it should bezero
else the return of@t
it should beone
SQL
query below
mysql> SELECT EXISTS
( SELECT @t := tt,
CASE WHEN ts = 0 THEN 0
ELSE 1 END
FROM `do_table` WHERE tt = 't_contents_2222_2_2021') AS COUNT INTO @t;
SELECT
@t;
Query OK, 1 row affected (0.00 sec)
+----+
| @t |
+----+
| 1 |
+----+
1 row in set (0.01 sec)
The return of @t
instead it’s always one
… whatever row is getting …
-- ----------------------------
-- Table structure for do_table
-- ----------------------------
DROP TABLE IF EXISTS `do_table`;
CREATE TABLE `do_table` (
`ts` int(11) DEFAULT NULL,
`tt` varchar(255) DEFAULT NULL,
`tID` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`tID`) USING BTREE
) ENGINE = InnoDB;
-- ----------------------------
-- Records of do_table
-- ----------------------------
INSERT INTO `do_table` VALUES (1, 't_contents_11111_2_2021', 1);
INSERT INTO `do_table` VALUES (0, 't_contents_2222_2_2021', 2);
INSERT INTO `do_table` VALUES (1, 't_contents_3333_2_2021', 3);