在同一列中的两个条件满足的情况下显示ID

时间:2019-10-14 00:25:13

标签: mysql sql

这是我的桌子1

html:

<div>
  <ul class = "mlist">
    <c:forEach var="rank" items="${moviedata }" varStatus="no">
      <li  value="${no.index }" id="ml_${no.index }">
        <a  href="../movielist/MDetail?mid=${rank.mid }" class = "mli" value="${no.index }">${rank.name }</a>
      </li>
    </c:forEach>
  </ul>
</div>

html result:

<div>
  <ul class = "mlist">

    <li  value="0" id="ml_0">
      <a  href="../movielist/MDetail?mid=1" class = "mli" value="0">moviename1</a>
    </li>

    <li  value="1" id="ml_1">
      <a  href="../movielist/MDetail?mid=11" class = "mli" value="1">moviename2</a>
    </li>

    <li  value="2" id="ml_2">
      <a  href="../movielist/MDetail?mid=25" class = "mli" value="2">moviename3</a>
    </li>

    <li  value="3" id="ml_3">
      <a  href="../movielist/MDetail?mid=27" class = "mli" value="3">moviename4</a>
    </li>

    <li  value="4" id="ml_4">
      <a  href="../movielist/MDetail?mid=5" class = "mli" value="4">moviename5</a>
    </li>

    <li  value="5" id="ml_5">
      <a  href="../movielist/MDetail?mid=23" class = "mli" value="5">moviename6</a>
    </li>

    <li  value="6" id="ml_6">
      <a  href="../movielist/MDetail?mid=3" class = "mli" value="6">moviename7</a>
    </li>

    <li  value="7" id="ml_7">
      <a  href="../movielist/MDetail?mid=4" class = "mli" value="7">moviename8 </a>
    </li>

    <li  value="8" id="ml_8">
      <a  href="../movielist/MDetail?mid=6" class = "mli" value="8">moviename9</a>
    </li>

    <li  value="9" id="ml_9">
      <a  href="../movielist/MDetail?mid=58" class = "mli" value="9">moviename10</a>
    </li>

  </ul>
</div>


JavaScript :

const all = ele => document.querySelectorAll(ele)
const one = ele => document.querySelector(ele)
const slide = _ => {
const wrap = one('.slide') 
const target = wrap.children[0]
const len = target.children.length

  target.style.cssText = width:calc(100% * ${len});display:flex;transition:1s

  Array.from(target.children)
  .forEach(ele => ele.style.cssText = width:calc(100% / ${len});)

  var i = 0
  var list = new Array()
$(".mli").each(function(){
  list.push($(this).attr("value"))
})

$()

  let pos = 0
  setInterval(() => {

    $(list[i]).css("color", "red") << //i don't know chage color

      if(i==9){
        i = 0
      }else{
        i++
      }

    pos = (pos + 1) % len 
    target.style.marginLeft = ${-pos * 100}% }, 3000)
}
window.onload = function () {
  slide()
}

查询-选择在Ram Rom中都至少有1个球的所有ID。

----所有在Ram中至少有1个球的ID

ID    || Basket  || Balls
--------------------------
1     || Ram   || 1  
1     || Rom   || 3  
2     || Ram   || 2  
3     || Rom   || 5   

---- Rom中至少有1个球的所有ID

 SELECT distinct  ID
 INTO #RAM
 FROM Table1
 where balls > =1 and basket = 'Ram'

---所有同时具有ID的ID

 SELECT distinct  ID
 INTO #ROM
 FROM Table1
 where balls > =1 and basket = 'Rom'

到目前为止,这是我所写的,但仍然有效,但是我认为确实存在更有效的方法。

请让我知道。谢谢。

4 个答案:

答案 0 :(得分:2)

我将使用单个聚合:

<body>
 <a href="/xxxx/aaaa" > AAAA </a>,  125.00  <br>
 <a href="/xxxx/bbbb" > BBBB </a>,  235.20  <br>
 <a href="/xxxx/cccc" > CCCC </a>,  145.04  <br>
</body>

假设select id from table1 t1 group by id having sum(case when basket = 'Ram' then balls end) > 0 and sum(case when basket = 'Rom' then balls end) > 0; 的值从不为负或零,那么您只需检查行是否存在。这在 MySQL 中更简单:

balls

答案 1 :(得分:0)

您可以使用subqueries来实现。

  
      
  1. 加入RAM
  2.   
  3. 加入ROM
  4.   
  5. 选择count()> = 1
  6.   
select t1.ID
from Table1 t1
join
    (select ID, count(1) ct from Table1 where Basket = 'Ram'
    group by ID) as ram on ram.ID = t1.ID
join
    (select ID, count(1) ct from Table1 where Basket = 'Rom'
    group by ID) as rom as rom.ID = t1.ID
where ram.ct >= 1 and rom.ct >= 1

答案 2 :(得分:0)

您只需要使用此查询并合并您的查询

SELECT 
  a.ID 
FROM (SELECT ID FROM Table1 Where Balls >= 1 And Basket = 'RAM') a
INNER JOIN (SELECT ID FROM Table1 Where Balls >= 1 And Basket = 'ROM') b 
ON a.ID = b.ID

仅返回大于1个球且位于RoM和RAM中的OD

答案 3 :(得分:0)

我们可以根据ID,购物篮进行分组,并选择计数大于1的记录

select distinct a.id from 
(
    select id,basket,count(balls) as cn
    from table1 
    group by id,basket
    having cn>1
) a ;