使用mysql行值作为mysql运算符(Use mysql row value as mysql operator)

编程入门 行业动态 更新时间:2024-10-24 18:28:17
使用mysql行值作为mysql运算符(Use mysql row value as mysql operator)

我有一张表,我在其中获得了我希望用作运算符的值:

+ value 1 + value 2 + operator + | --------+---------+----------| | value 1 | value 2 | = | | value 3 | value 4 | > | | value 5 | value 6 | < | +---------+---------+----------+

我想知道是否可以使用operator列作为WHERE子句的运算符来编写查询,例如:

SELECT * FROM table1 table2 operatorTable WHERE `table1`.`id` `operatorTable`.`operator` `table2`.`id`

此查询中的operatorTable.operator将分别替换为= , >或< 。

我的意思是,我知道如何在PHP和所有这些,但我想严格使用MySQL :)

目标是实现查询的最大速度。

I have a table in which I got values I wish to use as operators:

+ value 1 + value 2 + operator + | --------+---------+----------| | value 1 | value 2 | = | | value 3 | value 4 | > | | value 5 | value 6 | < | +---------+---------+----------+

I was wondering if it is possible to write a query using the operator column as the operator for WHERE clause, for example like this:

SELECT * FROM table1 table2 operatorTable WHERE `table1`.`id` `operatorTable`.`operator` `table2`.`id`

The operatorTable.operator in this query is to be substituted by =, > or < respectively.

I mean, I know how to do that in PHP and all that, but I'd like to use strictly MySQL :)

The goal is to achieve the maximum speed of the query.

最满意答案

您可以使用CASE运算符:

SELECT * FROM table1 table2 operatorTable WHERE CASE `operatorTable`.`operator` when '<' then `table1`.`id` < `table2`.`id` when '=' then `table1`.`id` = `table2`.`id` when '>' then `table1`.`id` > `table2`.`id` END CASE

(未经测试,但它应该工作)

You can use the CASE operator:

SELECT * FROM table1 table2 operatorTable WHERE CASE `operatorTable`.`operator` when '<' then `table1`.`id` < `table2`.`id` when '=' then `table1`.`id` = `table2`.`id` when '>' then `table1`.`id` > `table2`.`id` END CASE

(Not tested, but it should work)

更多推荐

本文发布于:2023-08-07 10:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463806.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:运算符   mysql   operator   row

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!