使用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)
更多推荐
发布评论