2012-10-15 89 views
4

我已經使用mysql連接三個表並將數據打印在HTML表中。我的查詢是這樣的:從MySQL連接查詢編輯結果

SELECT nid, title, cid, lid, street, city, state, cat_name, cat_icon 
FROM 
    node 
    JOIN location USING(nid) 
    LEFT JOIN categories USING(cid) 
ORDER BY nid DESC LIMIT 10 

然後我打印數據到一個很好的HTML表格。

這很好顯示我的數據,但我很好奇我怎麼可能去編輯結果。基本上,企業在「節點」表中有數據,而在「位置」中有地址信息。

我打算做一個模態或內聯編輯表單,但我很好奇你將如何更新數據,因爲它來自多個表?

+1

你剛剛描述了一個完整的CRUD(創建,讀取,更新,刪除)應用程序,請查看http://www.developerdrive.com/2012/06/create-your-own-crud-app-with-php-mysql-part-2/ –

+0

該鏈接非常有幫助,謝謝 – Jay

回答

4
UPDATE node JOIN location USING(nid) LEFT JOIN categories USING(cid) 
SET node.col1 = newvalue 

如mysql手冊所述,多表UPDATE語句可以使用SELECT語句允許的任何類型的連接,例如LEFT JOIN。

+0

正是我所需要的,謝謝 – Jay

1

[table_Business] BusinessID 節點ID LocationID BUSINESSNAME BusinessDescription

[table_Node] 節點ID 節點名 NodeDescription

[table_Location] LocationID 位置名稱 位置描述

表格之間的關係允許您修改/刪除/添加您的數據。關係由標識符(ID)決定。 在這種情況下,主表是table_Business。一個業務有一個節點和一個地點,它在關係圖中創建table_Node和table_Location次表。 對於一個業務,我們可以在所有三個表中都有數據:table_business table_node,table_location,但即使數據傳播了,我們仍然可以修改/引用它,只要我們有可以識別它的內容即可。這是businessID,nodeID或locationID起作用的地方。

通過了解NodeID我們可以修改一個業務的特定節點。

通過了解位置ID我們可以修改一個業務的特定位置。

通過了解BusinessID我們可以修改一個特定的業務,一個特定的數據和一個業務的特定位置。

(基於BusinessID,我們可以通過從table_Business中選擇LocationID或NodeID來獲取LocationID或NodeID。然後我們可以使用LocationID,節點ID在table_Location修改信息,分別table_Node)


這是同樣的數據有不同的關係定義

[table_Business] BusinessID 節點ID BUSINESSNAME BusinessDescription

[table_Node] NodeID 節點名 NodeDescription

[table_Location] LocationID BusinessID LOCATIONNAME LocationDescription

通知這裏,LocationID已從table_Business取出並BusinessID出現在table_Location。 通過這樣做,我們可以有多個位置的業務。如果您正在存儲肯德基類型的企業,有能力定義多個位置將是有益的。

通過了解NodeID我們可以修改一個業務的特定節點。

通過了解位置ID我們可以修改一個業務的特定位置。

通過了解BusinessID我們可以通過修改一個特定的業務,一個特定的節點但對於一個企業不是一個特定的位置

(如果我們使用BusinessID修改位置的數據,我們最終會修改該業務的所有位置。我們需要LocationID在這種情況下)

A relation is defined as a set of tuples that have the same attributes. A tuple usually represents an object and information about that object. Objects are typically physical objects or concepts. A relation is usually described as a table, which is organized into rows and columns. All the data referenced by an attribute are in the same domain and conform to the same constraints. The relational model specifies that the tuples of a relation have no specific order and that the tuples, in turn, impose no order on the attributes. Applications access data by specifying queries, which use operations such as select to identify tuples, project to identify attributes, and join to combine relations. Relations can be modified using the insert, delete, and update operators. New tuples can supply explicit values or be derived from a query. Similarly, queries identify tuples for updating or deleting. It is necessary for each tuple of a relation to be uniquely identifiable by some combination (one or more) of its attribute values. This combination is referred to as the primary key.

+0

謝謝你深思熟慮的解釋。我的表如下所示:'node:nid,title,cid location:nid,lid,street,city,state category:cid,cat_name,cat_icon'我能夠使用連接引用所需的數據,我只是不確定提交當數據來自3個地方時更新 – Jay