使用操作员使用mySQL查询的结果作为变量

发布于 2025-02-08 20:53:36 字数 1378 浏览 1 评论 0原文

寻找一种返回可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦言归人 2025-02-15 20:53:36

如果您真的,我的意思是真的知道这些变量不能是邪恶的,那么您可以构建和执行原始查询:

SET @export_date = '2022-06-20';
SET @order_ids = (SELECT GROUP_CONCAT(DISTINCT task_order SEPARATOR ',' )  FROM tasks WHERE task_execution_date = @export_date); 
SET @query = CONCAT('SELECT * from orders WHERE ordr_id IN (', @order_ids, ');');
PREPARE stmt FROM @query;
EXECUTE stmt;

If You really, really, I mean REALLY know that these variables can't be evil, then You can build and execute raw query:

SET @export_date = '2022-06-20';
SET @order_ids = (SELECT GROUP_CONCAT(DISTINCT task_order SEPARATOR ',' )  FROM tasks WHERE task_execution_date = @export_date); 
SET @query = CONCAT('SELECT * from orders WHERE ordr_id IN (', @order_ids, ');');
PREPARE stmt FROM @query;
EXECUTE stmt;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文