簡體   English   中英

多重表中帶有外鍵的自定義字段

[英]Custom fields with foreign keys in mutliple tables

[更新2:解決方案]:最后,解決方案是進行兩個不同的查詢:第一個查詢循環Items(沒有其自定義字段):

$itemsQuery = 'SELECT * FROM itemsTable";
$items = $db->Exectue($itemsQuery);

然后,在項目循環中,我進行第二次查詢以匹配正確的海關字段:

//FIRST LOOP => Render the items
while ($row = $items->FetchRow())
{

//Query the custom fields within the Items loop
$customFieldsQuery = 'SELECT item.id,
customFieldName.id, customFieldName.value as fieldName,
customFieldValue.id, customFieldValue.customFieldNameId customFieldValue.value as fieldValue

FROM itemsTable AS item
LEFT JOIN customFieldNameTable as customFieldName
    ON item.id = customFieldName.id
LEFT JOIN customFieldValueTable as customFieldValue
    ON customFieldValue.customFieldNameId = customFieldValue.id
WHERE item.id = '.$row['id'].'';

$customFields = $db->Execute($customFieldsQuery)

//Loop the custom fields BY item
while ($row2 = $fields->FetchRow()){
    // Show item title (unique)
     echo $row['title'] . "<br/>";
   //Show custom fields
    echo $row2['fieldName'] . " : " . $row2['fieldValue'];


}
}

[更新]不好,我忘記精確說明每個項目都有幾個自定義字段,所以聯合的問題是它呈現了多個項目(每個自定義字段一個。此外,Group_concat不是解決方案,因為我需要由於網站的用戶界面,因此可以獨立訪問每個自定義字段

假設我有3張桌子:

Table ITEMS store the id, title of an Item (Rows : id, title)

另外兩個表用於自定義字段:

  • FIELDNAME存儲了我們為自定義字段指定的名稱:行:id,id_item(用於檢索正確項目的外鍵),值

  • FIELDVALUE存儲我們提供給自定義字段的值,並具有一個外鍵來連接其來自FIELDNAME的名稱值:行:id,id_item(用於檢索正確項目的外鍵),id_fieldname(用於檢索正確字段名的外鍵) ),值

范例:

Table ITEMS :
id = 1
title = "title of the item"

Table FIELDNAME :
(id = 1)
id_item  = 1
value = "Country"

Table FIELDVALUE :
(id = 1)
id_item = 1
id_fieldname = 1
Value = "France"

我想做的是一個鏈接3個表的查詢,這樣我就可以執行以下操作:

while ($row = $myQuery->FetchRow())
{
    {$row['item_title']} => output "Title of the item"

    {$row['field.country']} => Output "Country: France"
}

$row['field.country']是fieldname的一種串聯:fieldval。

我幾乎用SQl的GROUP_CONCAT取得了不錯的結果,但是問題是,盡管我需要獨立地訪問每個自定義字段,但它卻將一行中的所有自定義字段都輸出了(類似

{$row['fieldname.country']} => "Country"
{$row['fieldname.country.value']} => "France"")

您可以在所有表​​之間創建聯接-但是如果要將結果提取到PHP數組中,則可能需要為字段加上別名。

您可能還需要反選FIELDNAME.valueFIELDVALUE.value因為value是MySQL中的保留字。

就像是:

SELECT 
ITEMS.id AS item_id,
ITEMS.title AS item_title,
FIELDNAME.id AS fieldname_id,
FIELDNAME.`value` AS fieldname_string,
FIELDVALUE.id AS fieldvalue_id,
FIELDVALUE.`value` AS fieldvalue_string

FROM ITEMS

INNER JOIN FIELDNAME
    ON FIELDNAME.id_item = ITEMS.id

INNER JOIN FIELDVALUE
    ON FIELDVALUE.id_item = ITEMS.id
    AND FIELDVALUE.id_fieldname = FIELDNAME.id

如果您使用mysqli_result::fetch_array(MYSQLI_ASSOC); 您應該將每一行作為一個數組結束,如下所示:

array (
    'item_id' => 1,
    'item_title' => "Title of the item",
    'fieldname_id' => 1,
    'fieldname_string' => "Country",
    'fieldvalue_id' => 1,
    'fieldvalue_string' => "France"
)


FIELDVALUE.id_item FIELDVALUE.id_fieldname ,如果FIELDVALUE.id_item + FIELDVALUE.id_fieldname是唯一的組合,則可以創建復合主鍵,而不是使用(大概)自動遞增的FIELDVALUE.id

使用以下查詢,您可以使用內部聯接獲取它

 SELECT i.title ,fn.value as filed_name,fd.value as field_value
 from ITEMS i inner join FIELDNAME fn 
on fn.id_item=i.id inner join FIELDVALUE fd on fd.id_fieldname = fn.id

因此,您將需要SQL Join

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID = Customers.CustomerID;

暫無
暫無

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

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