简体   繁体   English

语法 SELECT EXISTS 使用 MySql 版本 8.0.17

[英]Syntax SELECT EXISTS using MySql version 8.0.17

In the table do_table stored an a database MySql version 8.0.17 I have these rows在表do_table中存储了一个数据库MySql version 8.0.17我有这些行

+----+-------------------------+-----+
| 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:使用SQL我需要:

  1. Checking if table exists in the database because it may not have been created yet;检查database中是否存在表,因为它可能尚未创建;
  2. And if ts field is equal to zero the return of @t it should be zero else the return of @t it should be one如果ts字段等于zero ,@t 的返回值应该是zero ,否则@t的返回@t应该是one

SQL query below SQL查询下方

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.... @t的返回总是one ......无论行得到......

-- ----------------------------
-- 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);

For table creation, you can use IF NOT EXISTS :对于表创建,您可以使用IF NOT EXISTS

CREATE TABLE IF NOT EXISTS`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;

For the query, you can do:对于查询,您可以执行以下操作:

SELECT IF(ts=0, 0, 1) INTO @t
FROM `do_table` 
WHERE tt = 't_contents_2222_2_2021';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Pivot 在 MySQL 版本 8.0.17 中的行到列转换 - Row to column transformation in MySQL version 8.0.17 using Pivot MySql 版本 8.0.17 平均 datediff - MySql version 8.0.17 average datediff 分层数据使用数据库 MySql 版本 8.0.17 - Hierarchical-data using database MySql version 8.0.17 使用 MySql 版本 8.0.17 更新包含章节、段落和段落内容的表格 - Update table with chapters, paragraphs and paragraph content using MySql version 8.0.17 无法在 mysql 8.0.17 上授予选择 - Cannot Grant select on mysql 8.0.17 如何使用 MySQL 8.0.17 版本合并两个不同表的所有行? - How can I union all rows of two different tables using MySQL 8.0.17 version? 使用 Pivot 在 MySQL 版本 8.0.17 中从不同表(联合)进行行到列转换 - Row to column transformation from different tables (union) in MySQL version 8.0.17 using Pivot 使用 Pivot 在 MySQL 版本 8.0.17 中从不同表和不同行号(联合)进行行到列转换 - Row to column transformation from different tables and different rows number (union) in MySQL version 8.0.17 using Pivot 为什么 MySql 8.0.17 Select 查询性能比 Mysql 5.7.26 慢? - why MySql 8.0.17 Select query performance is slower than Mysql 5.7.26? MySql 版本 8.0.17 排除了 NOT LIKE 和 OR 条件的行 - MySql version 8.0.17 exclude rows where NOT LIKE and OR condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM