簡體   English   中英

SQL-JOIN錯誤=>多個表中的相同列名

[英]SQL-JOIN error => same column names in multiple tables

我在嘗試加入幾張桌子以獲得時間預訂系統時遇到了一些麻煩。

數據庫看起來像這樣:

數據庫表:

tbl_events

- int_eventID (INT)
- int_serviceID (INT)
- date_eventDueDate (DATETIME)
- date_eventCreationDate (DATETIME)
- int_userID (INT)
- int_customerID (INT)
- int_eventOnlineBooked (INT)

tbl_customers

- int_customerID (INT)
>>> - int_userID (INT) <= this one gives me headache << 
- str_customerFirstName (VARCHAR)
- str_customerLastName (VARCHAR)
- str_customerEmail (VARCHAR)
- str_customerPassword (VARCHAR)
- str_customerCellPhone (VARCHAR)
- str_customerHomePhone (VARCHAR)
- str_customerAddress (VARCHAR)

tbl_services

int_serviceID (INT)
str_serviceName (VARCHAR)
str_serviceDescription (VARCHAR)
int_servicePrice (INT)
int_serviceTimescale (TIME)

tbl_users

int_userID   (INT)
str_userFirstName (VARCHAR)
str_userLastName (VARCHAR)
str_userEmail (VARCHAR)
str_userPassword (VARCHAR)
str_userCellPhone (VARCHAR)

我已經通過SQL查詢按預期工作了(見下文)。 它為我提供了特定“特定用戶”在特定周內的所有事件。

SQL查詢:

SELECT  int_customerID as customerID,
    int_serviceID as serviceID,
    int_eventID as eventID,
    date_eventDueDate as eventDueDate,
    date_eventCreationDate as eventCreationDate,
    int_eventOnlineBooked as eventOnlineBooked,
    str_serviceName as serviceName,
    int_serviceTimescale as serviceTimescale,
    str_customerFirstName as customerFirstName,
    str_customerLastName as customerLastName,
    str_customerCellPhone as customerCellPhone,
    str_customerHomePhone as customerHomePhone
FROM tbl_events
JOIN tbl_services USING (int_serviceID)
JOIN tbl_customers USING (int_customerID)
WHERE
int_userID = 1 AND
YEARWEEK(date_eventDueDate,1) = 201219

問題是,我在tbl_customers表中沒有指定客戶所屬的用戶的列。 當我將“int_userID”添加到tbl_customers時,SQL停止了工作並給了我錯誤消息:

<b>Warning</b>:  mysql_num_rows() expects parameter 1 to be resource, boolean given in     <b>/Library/WebServer/Documents/calendar/api/calender_getWeekGetEvents.php</b> on line <b>46</b><br />

第46行:

if(mysql_num_rows($result)) {
    while($event = mysql_fetch_assoc($result)) {
        $events[] = $event;
    }
}

有任何想法嗎? :)

謝謝/ L

如果列名稱發生沖突,請嘗試指定您嘗試使用哪個表。

...
WHERE
tbl_Events.int_userID = 1 AND
...

在JOIN查詢中為表名使用別名是一種更好的做法,其中在多個表中使用相同的字段名稱。

暫無
暫無

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

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