在多个表中,SQL-JOIN错误=>相同的列名。

时间:2022-11-30 22:54:55

I have some trouble trying to join a couple of tables for a time-booking system.

我有一些麻烦,想要加入几个表格的时间预订系统。

The database look like this:

数据库是这样的:

Database tables:

tbl_events

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

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

tbl_services

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

tbl_users

tbl_users

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

I've got everything to work as expected by the SQL-query (see below). It gives me all events for a specific "user" during a specific week.

我已经按照SQL-query(见下)的期望工作了。它在特定的一周内为特定的“用户”提供所有事件。

SQL query:

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

The problem is, I didn't had a column in the tbl_customers table that specified which user that customer belongs to. When I added "int_userID" to tbl_customers the SQL-stopped working and gave me the error message:

问题是,我在tbl_customer表中没有指定客户属于哪个用户的列。当我向tbl_customers添加“int_userID”时,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 />

Line 46:

46行:

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

Any ideas? :)

什么好主意吗?:)

Thanks / L

谢谢/ L

2 个解决方案

#1


3  

If column names are colliding try to specify which table you try to use that from.

如果列名称发生冲突,请尝试指定要使用哪个表。

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

#2


1  

It is a better practice to use aliases for table name in JOIN query where same field name is used in multiple tables.

对于在多个表中使用相同字段名的JOIN查询中使用别名,是一种更好的做法。

#1


3  

If column names are colliding try to specify which table you try to use that from.

如果列名称发生冲突,请尝试指定要使用哪个表。

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

#2


1  

It is a better practice to use aliases for table name in JOIN query where same field name is used in multiple tables.

对于在多个表中使用相同字段名的JOIN查询中使用别名,是一种更好的做法。