簡體   English   中英

我們是否可以通過檢查SQL和HQL中的if else條件來設置表名?

[英]Can we set a tablename by checking a if else condition in SQL and as well as HQL?

我必須通過檢查table1中的一列來設置特定的表名(考慮多個表),在該列中將有一個String可以映射到該多個表中的任何一個。 因此,如果字符串與名稱匹配,則設置或檢查if else語句以設置特定的表名稱。

用簡單的語言

select tableNAme.name  
from yert  
if  (yert.name = 'hello' then set tableName = 'hello_table')  
else (yert.name = 'hi' then set tableName = 'hi_table')

RDBMS管理表和列。 因此,它使用保存表名,列名,列類型等的系統表。

您可以管理員工,物品,訂單,圖片等。 不管理表。 因此,您應該沒有包含表名的表。 (例外:您可能出於記錄目的而這樣做。)

因此,您似乎選擇了錯誤的數據模型,建議您進行更改。 但是,即使使用這樣的數據模型,也可以工作。 您將使用外部聯接來訪問鏈接的任何表。

select 
  yert.value, 
  coalesce(hello_table.description, hi_table.description)
from yert
left join hello_table 
  on yert.tablename= 'hello_table' and yert.table_id = hello_table.id
left join hi_table 
  on yert.tablename= 'hi_table' and yert.table_id = hi_table.id;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM