Mysql 5个表之间的连接问题

发布于 2024-11-30 00:49:07 字数 1094 浏览 6 评论 0原文

抱歉我的英语不好,我来自奥地利,但我找不到另一个不错的问答板。 :)

我有一个非常奇怪的结构化 mysql 数据库。 首先有一个用户表,对于我的示例,只有三列有趣:

id, name, email
  • 我的第二个表是事件表:id、name
  • 我的第三个表是事件的类别表:id、name、eventID
  • 我的第四个表是事件的下类别表:id,name,catID
  • 我的第五个表是事件的时间表:id,name,ucatID
  • 我的第六个表是一个表,其中一个条目将时间与人结合起来

它是工作计划,在某种程度上...

  1. 用户:id,名称,电子邮件
  2. 事件:id,名称
  3. 类别:id,名称,eventID
  4. 下类别:id,名称,catID
  5. 下类别:id,名称,ucatID
  6. 要加入的表:id,userID,uucatID

是的,到目前为止,每个条目 (用户名, ucat-name, uucat-name) ... 但我正在尝试获取在连接表中没有条目的人员列表! (但仅限于 eventID 为...例如 1^^)

这是我的列表代码,其中有一个条目:

SELECT a.*,b.name,c.name AS zeit,d.name AS kategorie
FROM intern_user AS b
INNER JOIN intern_dienstplan AS a ON a.userID=b.id
INNER JOIN intern_events_uucat AS c ON a.uucatID=c.id
INNER JOIN intern_events_ucat AS d ON c.ucatID=d.id
INNER JOIN intern_events_cat AS e ON d.catID=e.id
WHERE e.eventID='".$_POST['eventid']."'
ORDER BY b.name ASC

我希望有人可以帮助我...我已经尝试过使用“a.id is null” “(连接表中没有条目),但它不起作用。

Sorry for my english, I'm from austria, but I couldn't find another nice q&a-board. :)

I have a very strange structured mysql-database.
First there is a table for users, for my example there are only three colums interesting:

id, name, email
  • My second table is a table for events: id, name
  • My third table is a category-table for events: id, name, eventID
  • My fourth table is a undercategory-table for events: id, name, catID
  • My fifth table is a time-table for events: id, name, ucatID
  • My sixth table is a table, where a entry combines a time with a person

Its a plan for work, in some way...

  1. users: id, name, email
  2. events: id, name
  3. categories: id, name, eventID
  4. undercategories: id, name, catID
  5. underundercategories: id, name, ucatID
  6. table to join: id, userID, uucatID

Yeah I have, so far, every entry (user-name, ucat-name, uucat-name) ...
but I'm trying to get a list of persons who have no entry in the join-table! (but only, where the eventID is ... say 1^^)

Here is my code for the list where there is an entry:

SELECT a.*,b.name,c.name AS zeit,d.name AS kategorie
FROM intern_user AS b
INNER JOIN intern_dienstplan AS a ON a.userID=b.id
INNER JOIN intern_events_uucat AS c ON a.uucatID=c.id
INNER JOIN intern_events_ucat AS d ON c.ucatID=d.id
INNER JOIN intern_events_cat AS e ON d.catID=e.id
WHERE e.eventID='".$_POST['eventid']."'
ORDER BY b.name ASC

I hope someone can help me... I have already tried something with "a.id is null" (where is no entry in join-table), but it doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

放赐 2024-12-07 00:49:07

很长一段时间过去了......我已经找到了解决我的问题的方法。 :]

这里是代码,供其他有同样问题的人使用...

SELECT *
FROM intern_user
WHERE 
(
    id NOT IN 
    (
            SELECT a.userID
        FROM intern_dienstplan AS a
        INNER JOIN intern_events_uucat AS b ON a.uucatID=b.id
        INNER JOIN intern_events_ucat as c ON b.ucatID=c.id
        INNER JOIN intern_events_cat AS d ON c.catID=d.id
                WHERE d.eventID='".$_POST['eventid']."'
        GROUP BY a.userID
        HAVING sum(b.wert) >= 100
    )
)
AND status='1'

感谢所有帮助,祝你玩得开心。 :D

问候

A long time has gone... and I have found the solution for my problem. :]

Here the code, for others with the same problem...

SELECT *
FROM intern_user
WHERE 
(
    id NOT IN 
    (
            SELECT a.userID
        FROM intern_dienstplan AS a
        INNER JOIN intern_events_uucat AS b ON a.uucatID=b.id
        INNER JOIN intern_events_ucat as c ON b.ucatID=c.id
        INNER JOIN intern_events_cat AS d ON c.catID=d.id
                WHERE d.eventID='".$_POST['eventid']."'
        GROUP BY a.userID
        HAVING sum(b.wert) >= 100
    )
)
AND status='1'

Thanks for all help and have fun with this shit. :D

Greets

演出会有结束 2024-12-07 00:49:07

目前尚不清楚类别-子类别-子子类别的连接类型是什么。
有多少子类别可以有该事件 (0-1-N)?

如果你只是想查找链接表中没有匹配记录的人,你可以使用查询:

select a.*
from user a
left join link b on a.id = b.userId
where b.userId is null

select a.*
from user a
where id not in (select userId from link)

更新。上述问题仍然没有答案。
你可以尝试:

select *
from users
where id not in (
    select link.userId
    from event a
    inner join categories ...
    inner join subcategories ...
    inner join categories ...
    inner join link ...
    where a.eventId = ...)

它可以很容易地转换为带有 left join 或 contains() 的版本。

It's not clear what is type of connection for categories-subcategories-subsubcategories.
How much subcategories could have the event (0-1-N)?

If you just want to find persons who do not have matched record in link table, you can use a query:

select a.*
from user a
left join link b on a.id = b.userId
where b.userId is null

or

select a.*
from user a
where id not in (select userId from link)

Update. Still do not have answer for the question above.
You can try:

select *
from users
where id not in (
    select link.userId
    from event a
    inner join categories ...
    inner join subcategories ...
    inner join categories ...
    inner join link ...
    where a.eventId = ...)

It could be easy transformed to versions with left join or exists().

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