繁体   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