简体   繁体   English

选择与查询返回的结果相反的结果

[英]Selecting the opposite to the results returned by query

I am trying to simplify my code. 我正在尝试简化我的代码。

I currently have two querys: one selecting the list of information and outputing it with select boxes to add to a users subscriptions then a second query to select the items that user already has subscribed but I am trying to simplify the code to one query. 我目前有两个查询:一个选择信息列表并使用选择框将其输出以添加到用户订阅中,然后第二个查询选择用户已订阅的项目,但是我试图将代码简化为一个查询。

I can currently get the code to display the options that the user has subscribed to with check boxes but not the opposite is there a way to select the opposite to the results in a WHERE clause? 目前,我可以获取代码以显示用户已使用复选框订阅的选项,但不是相反的方法,是否可以在WHERE子句中选择与结果相反的方法?

$selectionQuery = 'SELECT t1.subId, t1.subTitle FROM tbl_subs t1 LEFT JOIN tbl_list t2 ON t2.subId = t1.subId WHERE t2.userId = '.$id.' AND t1.subId = t2.subId'; 

You already joining the subId at the LEFT JOIN part. 您已经在LEFT JOIN部分加入了subId。 Remove the AND t1.subId = t2.subId' from the WHERE part, as it joins again your resultset and overcomes the left join. WHERE部分中删除AND t1.subId = t2.subId' ,因为它再次连接您的结果集并克服了左连接。

Correct: 正确:

$selectionQuery = 'SELECT t1.subId, t1.subTitle FROM tbl_subs t1 LEFT JOIN tbl_list t2 ON t2.subId = t1.subId WHERE t2.userId = '.$id.'; 

This code maybe is not ansiSQL but it works perfect on MYSQL. 此代码可能不是ansiSQL,但在MYSQL上可以完美运行。

$selectionQuery = 'SELECT t1.subId, t1.subTitle FROM tbl_subs as t1 WHERE not exists (select 1 from tbl_list as t2 where t2.userId = '.$id.' AND t1.subId = t2.subId)'; $ selectionQuery ='从tbl_subs中选择t1.subId,t1.subTitle作为t1,但不存在(从tbl_list中选择1作为t2,其中t2.userId ='。$ id。'AND t1.subId = t2.subId)';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM