English 中文(简体)
混淆了我sql提出的复杂合并请求
原标题:confused about complicated join request in mysql

Basically I have 4 tables and the group id of the current group. I ll list the important fields and info:

Users:
ID,
FirstName,
LastName

Groups:
ID,
participantlist_id

participantlist:
list_id

participantlist_links:
list_id,
participant_id

我已经指出,如何显示参与人名单上的所有用户。 list 在树顶点,但功能不大:

SELECT Users.ID,Users.FirstName,Users.LastName FROM Users
JOIN participantlist_links ON Users.ID = participantlist_links.participant_id
JOIN participantlist ON participantlist_links.list_id = participantlist.list_id
JOIN Groups ON participantlist.list_id = Groups.participantlist_id AND 
Groups.ID =  ".workspaces_tell_me_brainstorm_group_id_from_group_id($group_id)." 

我现在要排除与参与人名单有关联的所有用户。 清单——现组(组)——但我仅能以一个单一问题表明这样做的良好方式。 我认为,解决办法将是某种左翼加入/右边或左边/右边同比。

我的临时工作,但非常昂贵的解决办法是:

SELECT table2.id as ID, table2.firstname as FirstName, table2.lastname as LastName
FROM

(SELECT table1.ID,table1.FirstName,table1.LastName FROM Users as table1
JOIN participantlist_links ON table1.ID = participantlist_links.participant_id
JOIN participantlist ON participantlist_links.list_id = participantlist.list_id
JOIN Groups ON participantlist.list_id = Groups.participantlist_id 
AND Groups.ID =  ".workspaces_tell_me_brainstorm_group_id_from_group_id($group_id)." )
as table2

LEFT JOIN
(SELECT table4.ID,table4.FirstName,table4.LastName FROM Users as table4
JOIN participantlist_links ON table4.ID = participantlist_links.participant_id
JOIN participantlist ON participantlist_links.list_id = participantlist.list_id
JOIN Groups ON participantlist.list_id = Groups.participantlist_id 
AND Groups.ID =  ".$group_id." ) 
as table3

ON table2.ID = table3.ID WHERE table3.id is null

Any suggestions?

最佳回答

我只谈一点一点简单。

我想不是 。 它应当使你的法典更加容易阅读,而且最有可能让我的智慧更加优化。

SELECT table1.ID,
       table1.FirstName,
       table1.LastName 
  FROM Users as table1
  JOIN participantlist_links ON table1.ID = participantlist_links.participant_id
  JOIN participantlist ON participantlist_links.list_id = participantlist.list_id
  JOIN Groups ON participantlist.list_id = Groups.participantlist_id 
   AND Groups.ID =  ".workspaces_tell_me_brainstorm_group_id_from_group_id($group_id)." )
   AND table1.id not in (
               SELECT table4.ID
                 FROM Users as table4
                 JOIN participantlist_links ON table4.ID = participantlist_links.participant_id
                 JOIN participantlist ON participantlist_links.list_id = participantlist.list_id
                 JOIN Groups ON participantlist.list_id = Groups.participantlist_id 
                  AND Groups.ID =  ".$group_id." 
                )
问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签