[英]Mysql query to get rows of a table as columns of another, with column names from third table
[英]mysql: get table names from another query
對此也有類似的問題,但我無法完全解決。 我有2個數據庫。 db 1具有用戶名
例如
userdb.usertable.username
db 2具有每個用戶的配置表,這些表稱為
"their username" + _Config
我正在嘗試運行一個查詢,該查詢將遍歷特定類型的所有用戶,獲取其用戶名,然后從其自己的配置表中提取一個值。
這很接近,但僅適用於1位用戶。
SET @username:=(SELECT userdb.usertable.username FROM userdb.usertable WHERE version="free" );
SET @table:=concat(@username,"_Config");
SET @sql_text =concat("SELECT * FROM configdb.",@table);
PREPARE stmt FROM @sql_text;
EXECUTE stmt;
任何幫助,將不勝感激。
首先: 立即 消除 此數據庫設計。
99%的時間,您不應該為單個應用程序使用兩個數據庫。
您可能還想看看此內容,然后再繼續。
其次 ,應該是這樣的:
Users_Table
id,username,(more user detail fields),config_value_1,config_value_2
將所有用戶數據保存在一個表中,除非用戶的config參數可以有多個值。 然后,您將需要一個這樣的聯結表
Users_Table
id,username,other fields
Config_Value_1_Table
value_id,name
Config_Value_1_Users
user_id,value_id
現實世界中的一個常見示例如下所示:
users
id,first_name,last_name,username,password_hash,password_salt,email,is_admin,is_banned,is_active
locations
id,location_name
user_locations
user_id,location_id
然后您要做的就是編寫一個非常簡單的查詢以獲取所有用戶數據
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.