使用操作员使用mySQL查询的结果作为变量
寻找一种返回可以在 operator中内使用的变量的方法。
我目前的结果返回:1,2,3
, 但是我猜该变量应该更像是这样:'1','2','3'
能够使用它。
这可能吗?
还是我应该尝试其他一些事情,例如爆炸,保存查询,...
-- init vars
SET @export_date = '2022-06-20';
SET @order_ids = '';
-- Tasks on given day
SELECT * FROM tasks WHERE task_execution_date = @export_date;
-- method 1
SET @order_ids = (SELECT GROUP_CONCAT(DISTINCT task_order SEPARATOR ',' ) FROM tasks WHERE task_execution_date = @export_date);
SELECT @order_ids; -- "1,2,3"
-- method 2
SET @order_ids = (SELECT GROUP_CONCAT(DISTINCT '' + cast( task_order AS CHAR(50)) +'' ) FROM tasks WHERE task_execution_date = @export_date);
SELECT @order_ids; -- "1,2,3"
-- method 3
SET @order_ids = (SELECT GROUP_CONCAT( DISTINCT + '\'' + CAST(task_order AS CHAR (100)) + '\'') FROM tasks WHERE task_execution_date = @export_date);
SELECT @order_ids; -- "1,2,3"
-- Orders by tasks
SELECT * from orders WHERE ordr_id IN (@order_ids); -- returns only one result
-- SELECT * from orders WHERE ordr_id IN @order_ids; -- error
SELECT * from orders WHERE ordr_id IN ('1', '2', '3'); -- works
SELECT * from orders WHERE ordr_id IN (SELECT DISTINCT task_order FROM tasks WHERE task_execution_date = @export_date); -- works
-- Also needed:
-- goods by orders
-- good_adrs by goods
Looking for a way to return a variable that can be used inside a IN
-operator.
My current result returns: 1,2,3
,
but I guess the variable should be more like this: '1','2','3'
to be able to use it.
Is this possible?
Or should I try something else like explode, save the query,...
-- init vars
SET @export_date = '2022-06-20';
SET @order_ids = '';
-- Tasks on given day
SELECT * FROM tasks WHERE task_execution_date = @export_date;
-- method 1
SET @order_ids = (SELECT GROUP_CONCAT(DISTINCT task_order SEPARATOR ',' ) FROM tasks WHERE task_execution_date = @export_date);
SELECT @order_ids; -- "1,2,3"
-- method 2
SET @order_ids = (SELECT GROUP_CONCAT(DISTINCT '' + cast( task_order AS CHAR(50)) +'' ) FROM tasks WHERE task_execution_date = @export_date);
SELECT @order_ids; -- "1,2,3"
-- method 3
SET @order_ids = (SELECT GROUP_CONCAT( DISTINCT + '\'' + CAST(task_order AS CHAR (100)) + '\'') FROM tasks WHERE task_execution_date = @export_date);
SELECT @order_ids; -- "1,2,3"
-- Orders by tasks
SELECT * from orders WHERE ordr_id IN (@order_ids); -- returns only one result
-- SELECT * from orders WHERE ordr_id IN @order_ids; -- error
SELECT * from orders WHERE ordr_id IN ('1', '2', '3'); -- works
SELECT * from orders WHERE ordr_id IN (SELECT DISTINCT task_order FROM tasks WHERE task_execution_date = @export_date); -- works
-- Also needed:
-- goods by orders
-- good_adrs by goods
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您真的,我的意思是真的知道这些变量不能是邪恶的,那么您可以构建和执行原始查询:
If You really, really, I mean REALLY know that these variables can't be evil, then You can build and execute raw query: