0

I have two tables: Users and Groups

In my table "Users", there is a column called "ID" for all the user ids.

In my table "Groups" there is a column called "Participants", fields in this column are filled with all the user ids like this "PID_134,PID_489,PID_4784," - And there is a column "ID" that identifies a specific group.

Now what i want to do, i want to create a menu that shows all the users that are not yet in this particular group.

So i need to get all the user ids, that are not yet in the Participants column of a group with a particular ID.

It would be cool if there was a single mysql query for that - But any PHP + MySQL solutions are okay, too.

How does that work? Any guesses?

UPDATE:
i know, that's not code, but is there a way I could do something like this that would return me a list of all the users?

SELECT * 
FROM users, groups 
WHERE groups.participants NOT LIKE '%PID_'users.id'%' AND groups.id = 1;
4

3 に答える 3

0

以下に示すように、複数のテーブルから選択できます。

SELECT * from users, groups WHERE users.id != groups.participants AND groups.id = 1;

これにより、グループID1に属していないすべてのユーザーが一覧表示されます。結合を使用することで、より洗練された解決策を見つけることができますが、これは単純であり、うまくいきます。

于 2012-04-19T14:56:32.310 に答える
0

このようなもの。IDの「PID_」の部分を削除するだけです。

SELECT * FROM [users] WHERE [id] NOT IN 
(SELECT replace(id,'PID_','') FROM groups WHERE group_name='group1')

Group1は変数になります-開いたメニューのグループID/名前。

于 2012-04-19T16:08:11.530 に答える
0

私はそのようなものが役立つと信じています:

SELECT * FROM users WHERE users.id NOT IN (SELECT groups.participants FROM groups)

ただし、これは DB が正規化されている場合にのみ機能します。したがって、あなたの場合、PHP + MySQL ソリューションのみが表示されます。あまりエレガントではありませんが、仕事はします。

<?php
$participants_array = mysql_query("SELECT participants FROM groups");
$ids = array();
while ($participant = mysql_fetch_assoc($participants_array))
{
  $id = explode(',', $participant['participant']);
  foreach ($id as $instance)
  {
    if (!in_array($instance, $ids)) $ids[] = $instance;
  }
}
$participants = implode(',', $ids);
$result = mysql_query("SELECT * FROM users WHERE id NOT IN ( $participants )");

ただし、データベースを正規化することを強くお勧めします。

于 2012-04-19T15:37:34.287 に答える