سؤال

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.

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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().

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top