如何交叉两个联合查询

时间:2017-09-08 07:20:30

标签: sql union

您好我有两个查询

一个是

SELECT userid_one AS id FROM `friends` WHERE `userid_two` = '$UserId' AND `requeststatus` ='accepted'
                    UNION 
SELECT userid_two AS id FROM `friends` WHERE  `userid_one` =  '$UserId' AND `requeststatus` ='accepted'

它给了我用户ID 41,38,61,62,64

第二是

SELECT DISTINCT  `userid` FROM  `groupmembers` WHERE  `groupid` =24

它给了我61,49,62

我想要结果用户ID他们不在两个结果中 即:49,41,38,64,38

我该怎么做

5 个答案:

答案 0 :(得分:1)

尝试使用此查询:

select id
from (your union)
group by id
having count(*) = 1

答案 1 :(得分:1)

给你的第一个结果(联合)一个A的别名,给你的第二个结果别名为B,

然后你可以这样做:

SELECT id 
WHERE ((id IN A) AND NOT (id IN B)) OR (NOT (id IN A) AND (id IN B))
FROM (A UNION B)

答案 2 :(得分:0)

试试这个

SELECT * FROM (

SELECT userid_one AS id FROM `friends` WHERE `userid_two` = '$UserId' AND `requeststatus` ='accepted'
                    UNION 
SELECT userid_two AS id FROM `friends` WHERE  `userid_one` =  '$UserId' AND `requeststatus` ='accepted'

) as t ORDER BY id

答案 3 :(得分:0)

在第一个结果(带连接的结果)和第二个结果之间取UNION ALL。 这样重复的值仍然存在。现在使用GROUP BY查找非重复的ids

SELECT id from 
       ( SELECT userid_one AS id FROM `friends` 
         WHERE `userid_two` = '$UserId' AND `requeststatus` ='accepted'

         UNION 

        SELECT userid_two AS id FROM `friends` 
        WHERE  `userid_one` =  '$UserId' AND `requeststatus` ='accepted'

        UNION ALL

        SELECT DISTINCT  `userid`  AS id FROM  `groupmembers` WHERE  `groupid` =24
       ) A

        GROUP BY id having COUNT(1)=1

答案 4 :(得分:0)

我不知道你的数据库引擎。但是,由于JOIN在所有人中都很常见,我希望以下代码可以为您服务

i try <?php 
session_start();
include "mysql.php"; 
?>
<?php 
$cartOutput = "";
$product_id_array = '';
if (isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) > 1) { 
  // Start the For Each loop
   $i = 0; 
    foreach ($_SESSION["cart_array"] as $each_item) { 
      $item_id = $each_item['item_id'];
     $qty = $each_item['quantity'];
}
      $sql = mysql_query("SELECT * FROM products WHERE id='$item_id' ORDER BY id");
      while ($row = mysql_fetch_array($sql)) {
       $product_name = $row["product_name"];
// Create the product array variable
      $product_id_array .= "$item_id-".$each_item['quantity'].","; 
      // Dynamic table row assembly
  for ($b = 1; $b <= $each_item['quantity']; $b++) {
         $cartOutput .= "<tr>";
      $cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name. '" width="40" height="52" border="1" /></td>';
      //$cartOutput .= '<td>' . $each_item['quantity'] . '</td>';
      $cartOutput .= '</tr>';
      $i++; 
    }}
?>
    //output each variable with diffrent code from fetch query
      <?php $query_select = ("SELECT code, FROM diffrent_codes WHERE name='$product_name' ORDER BY id ASC"); $result_select = $mysqli_query($query_select) or die(mysql_error()); $rows = array(); while($row = mysql_fetch_array($result_select)) $rows[] = $row; foreach($rows as $row){ $code = $row['code']; } echo $cartOutput$code; } ?

 ?>
my $cartOutput$code to output diffrent $code according to each session quantity variable

如果您使用的是MySQL,则必须搜索 FULL OUTER JOIN

的解决方法