MySql依據多個ID查詢

2021-08-13 18:09:03 字數 1532 閱讀 3367

create table tbl_user_dtl

(user_id varchar(32),

user_name varchar(50)

);create table tbl_user_inf

(tu_id varchar(32),

user_ids varchar(2000)

);delimiter $$

create function test_my_sql(strs varchar(2000)) returns varchar(2000)

begin

declare cnt_names varchar(2000) default '';

declare cur_name varchar(50) default '';

declare empid varchar(32) default '';

declare last_id varchar(32) default '';

if locate(',', strs) <= 0 then

#單個id取出資訊

select

user_name

into cnt_names from

tbl_user_dtl

where

user_id

= strs;

else

#取出前面的id資訊

while locate(',', strs) > 0 do

select substr(strs, 1, locate(',', strs) - 1) into empid;

select

user_name

into cur_name from

tbl_user_dtl

where

user_id

= empid;

select substr(strs, locate(',', strs) + 1, char_length(strs)) into strs;

set cnt_names = concat(cnt_names,cur_name,',');

set cur_name = '';

#取出最後乙個id

select substr(strs, locate(',', strs) + 1, char_length(strs)) into last_id;

end while;

select

user_name

into cur_name from

tbl_user_dtl

where

user_id

= last_id;

set cnt_names = concat(cnt_names,cur_name);

end if;

return cnt_names;

end$$

delimiter ;

例:查詢結果如下圖所示

目前該函式不能泛用,且表名需要更改等已知問題。由於對mysql了解不多,目前只能做到這個程度了。歡迎指正,謝謝。

mysql 多個 Id MySQL查詢多個ID

從您的問題來看,我相信您當前的表結構如下 table user table project table shared id email id user id content id user id project id 1 james website.com 1 1 project for james...

mysql將查詢到的多個id合併成id字串

開發介面遇到乙個情景,查詢符合條件的id,將查詢出來的id作為字串更新到另乙個欄位中。但查詢到的結果是這樣的 需要將查詢到的值合併成字串,更新到另乙個欄位中,用函式group concat完美解決。將查詢到的id語句加上這個函式即可。update asm vip passenger set acco...

mysql外來鍵是多個id組成的字串,查詢方法

借鑑 mysql使用instr達到in 字串 的效果 結論 select from 表名where instr concat 字串 concat 表id 該錶的設計連第一正規化都沒有實現,不能容忍!表一 表二 首先想到的思路,對字串進行遍歷查詢,但是mybatis中collection不接受stri...