当我连接 2 个表时出现 SQL 错误

发布于 2024-12-05 21:32:06 字数 933 浏览 0 评论 0原文

抱歉让我修改一下。我有三个表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别靠近我心 2024-12-12 21:32:06

这些通知几乎是不言自明的。结果集中没有 'Year''EventName' 字段。很难(或者:不可能)说出为什么会发生这种情况,因为您没有给出表结构,但我猜想:'Year'date-table, 'EventName'event-table 的一个字段 - 您仅从 members 中进行选择,因此此字段不会发生。

我不明白为什么有三个 sql 语句,但只有一个被分配给变量 - 其他两个只是站在那里,什么也不做。请解释这一点,并在您的问题中加入更多信息,说明您想要实现的目标、您的表结构是什么样的以及您的预期结果是什么。

我认为您真正想做的是某种连接查询,所以请看一下 文档以了解其工作原理。

最后,我认为您的查询应该如下所示:

SELECT
  *
FROM
  members
INNER JOIN
  events_year ON members.id = events_year.id
INNER JOIN
  Event ON Event.EventID = events_year.EventID
INNER JOIN
  ´Date´ ON ´Date´.YearID = events_year.YearID
WHERE
  members.username = '$un'

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 the date-table, 'EventName' is a field of the event-table - you're only selecting from members 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:

SELECT
  *
FROM
  members
INNER JOIN
  events_year ON members.id = events_year.id
INNER JOIN
  Event ON Event.EventID = events_year.EventID
INNER JOIN
  ´Date´ ON ´Date´.YearID = events_year.YearID
WHERE
  members.username = '$un'
三生池水覆流年 2024-12-12 21:32:06

查询输出中是否存在“年份”字段?我怀疑不是。

Does the field 'Year' exist in the query output ? I suspect not.

北城孤痞 2024-12-12 21:32:06

字符串 $query 仅使用文本的第一行:

    "SELECT * FROM members JOIN events_year ON members.id = events_year.id ";

而不使用其他文本。

查询本身不返回任何名为 Year 或 EventName 的字段。

执行 var_dump($row) 来找出返回的内容。

the string $query is only using the first line of text:

    "SELECT * FROM members JOIN events_year ON members.id = events_year.id ";

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文