如何在sql中检查表中是否存在所有元素?

时间:2017-10-04 05:00:31

标签: sql oracle

我在PHP中有任何主题ID数组,比如说

['abc123', 'bcd3432', 'bla232', ....]

这是我在sql oracle中的错误尝试

select
  *
from
  subject_table
where
  subject_id IN ('abc123', 'bcd3432', 'bla232'....)

如果主题在那里,请返回true否则返回false

我如何实现它?

5 个答案:

答案 0 :(得分:1)

您需要一个明确的计数才能知道所有subject_id是否存在。

select case when count(distinct subject_id) = 3 then 'true' else 'false' end as status 
  from subject_table
 where subject_id in('abc123', 'bcd3432', 'bla232')

答案 1 :(得分:0)

尝试:

dict

答案 2 :(得分:0)

如果您设置> 0为真,0为假,您可以这样选择:

UIImage *image = [UIImage imageNamed:@"headr_bg"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

答案 3 :(得分:0)

将数组作为集合传递:

<head>
  <base href="https://polygit.org/polymer+1.10.1/components/">
  <script src="webcomponentsjs/webcomponents-lite.js"></script>
  <link rel="import" href="polymer/polymer.html">
  <link rel="import" href="neon-animation/web-animations.html">
  <link rel="import" href="iron-form/iron-form.html">
  <link rel="import" href="paper-dropdown-menu/paper-dropdown-menu.html">
  <link rel="import" href="paper-listbox/paper-listbox.html">
  <link rel="import" href="paper-item/paper-item.html">
</head>
<body>
  <x-foo></x-foo>

  <dom-module id="x-foo">
    <template>
      <iron-form id="myForm" on-iron-form-response="_onResponse">
        <form action="https://httpbin.org/get">

          <paper-dropdown-menu id="menu" label="Minimum Price" name="minimumPrice">
            <paper-listbox slot="dropdown-content" class="dropdown-content">
              <paper-item label="0">No min</paper-item>
              <paper-item label="50">$50 dollars</paper-item>
              <paper-item label="100">$100 dollars</paper-item>
              <paper-item label="200">$200 dollars</paper-item>
            </paper-listbox>
          </paper-dropdown-menu>

          <button type="submit">Submit</button>
        </form>
      </iron-form>
      
      <pre>[[response]]</pre>
    </template>
  </dom-module>
</body>

然后你可以这样做:

CREATE OR REPLACE TYPE StringList IS TABLE OF VARCHAR2(20);
/

您甚至可以使用bind parameter to pass the array in

SELECT CASE
       WHEN EXISTS (
              SELECT 1
              FROM   subject_table
              WHERE  StringList( 'abc123', 'bcd3432', 'bla232' )
                     SUBMULTISET OF
                     CAST( COLLECT( subject_id ) AS StringList )
            )
       THEN 'true'
       ELSE 'false'
       END AS has_all_ids
FROM   DUAL;

答案 4 :(得分:0)

你可以试试这个。在这里我将包含所有元素的字符串拆分为一组行,并将其与主表与LEFT JOIN

进行比较
WITH     given_sub
     AS (    SELECT DISTINCT TRIM (REGEXP_SUBSTR ('abc123,bcd3432,bla232',
                                                  '[^,]+',
                                                  1,
                                                  LEVEL))
                                subject_id
               FROM DUAL
         CONNECT BY INSTR ('abc123,bcd3432,bla232',
                           ',',
                           1,
                           LEVEL - 1) > 0)
SELECT CASE
          WHEN COUNT (DISTINCT s.subject_id) < COUNT (DISTINCT g.subject_id)
          THEN
             'FALSE'
          ELSE
             'TRUE'
       END
          All_found
  FROM given_sub g
       LEFT OUTER JOIN subject_table s
          ON     s.subject_id = g.subject_id
             AND s.subject_id IN ('abc123',
                                  'bcd3432',
                                  'bla232'
                                  );