当我连接 2 个表时出现 SQL 错误
抱歉让我修改一下。我有三个表:
events_year
• 事件ID
• 年号
• id
日期
• 年号
• 年
事件
• 事件ID
• 事件名称
• EventType
我想显示三个表中的记录,如下所示:
EventName - Year: Marathon - 2008
我将其链接到一个名为“members”的表,该表包含一个 ID 号字段(members-id),
以便我可以限制将结果发送给members id = $un(这是会话中的用户名)
我需要连接三个表并将结果限制为特定的 ID 号记录
这是我的代码部分:
$query = "SELECT * FROM members JOIN events_year ON members.id = events_year.id ";
"SELECT * FROM Event JOIN events_year ON Event.EventID = events_year.EventID WHERE username = '$un'";
"SELECT * FROM Date JOIN events_year ON Date.YearID = events_year.YearID WHERE username = '$un'";
$results = mysql_query($query)
or die(mysql_error());
while ($row = mysql_fetch_array($results)) {
echo $row['Year'];
echo " - ";
echo $row['Event'];
echo "<br>";
}
Sorry let me revise. I have a three tables:
events_year
• EventID
• YearID
• id
Date
• YearID
• Year
Event
• EventID
• EventName
• EventType
i want to dispay a record from the three tables like so:
EventName - Year: Marathon - 2008
i linked it to a table called "members" which contains a ID number field (members-id)
so i can limit the results to members id = $un(which is a username from a session)
I need to join the three tables and limit the results to the specific ID number record
Here is my portion of the code:
$query = "SELECT * FROM members JOIN events_year ON members.id = events_year.id ";
"SELECT * FROM Event JOIN events_year ON Event.EventID = events_year.EventID WHERE username = '$un'";
"SELECT * FROM Date JOIN events_year ON Date.YearID = events_year.YearID WHERE username = '$un'";
$results = mysql_query($query)
or die(mysql_error());
while ($row = mysql_fetch_array($results)) {
echo $row['Year'];
echo " - ";
echo $row['Event'];
echo "<br>";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这些通知几乎是不言自明的。结果集中没有
'Year'
和'EventName'
字段。很难(或者:不可能)说出为什么会发生这种情况,因为您没有给出表结构,但我猜想:'Year'
是date-table,
'EventName'
是event
-table 的一个字段 - 您仅从members
中进行选择,因此此字段不会发生。我不明白为什么有三个 sql 语句,但只有一个被分配给变量 - 其他两个只是站在那里,什么也不做。请解释这一点,并在您的问题中加入更多信息,说明您想要实现的目标、您的表结构是什么样的以及您的预期结果是什么。
我认为您真正想做的是某种连接查询,所以请看一下 文档以了解其工作原理。
最后,我认为您的查询应该如下所示:
the notices are almost self-explaining. There are no
'Year'
and'EventName'
fields in the resultset. It's difficult (or: impossible) to tell why this happens as you haven't given your table-structure, but i guess this:'Year'
is a field of thedate
-table,'EventName'
is a field of theevent
-table - you're only selecting frommembers
so this fields don't occur.I don't understand why there are three sql-statements but only one is assigned to a variable - the other two are just standing there and do nothing. Please explain this and put more information into your question about what you're trying to achive, what your table-structure looks like and whats your expected result.
I think what you really wanted to do is some kind of joined query, so please take a look at the documentation to see how this works.
finally, i think your query should look like this:
查询输出中是否存在“年份”字段?我怀疑不是。
Does the field 'Year' exist in the query output ? I suspect not.
字符串 $query 仅使用文本的第一行:
而不使用其他文本。
查询本身不返回任何名为 Year 或 EventName 的字段。
执行 var_dump($row) 来找出返回的内容。
the string $query is only using the first line of text:
and not the others.
The query itself is not returning any fields that are called Year or EventName.
Do a var_dump($row) to find out what is being returned.