Querys MYSQL BELONGS

Asked

Viewed 50 times

-2

I need to make a query in the mysql, within the WHERE I need you to contain the COD_IDENT within a group. That is:

COD_IDENT 
5
6
7

My query need to return all results where COD_IDENT be equal to 5,6,7

I tried to do using in but it returns only one result. Actually it was to return two.

I did it this way:

SELECT COD_IDENT_REUNI, COD_IDENT_SALAX 
  FROM tbl_REUNIOES WHERE COD_IDENT_EMPRE = 'ibhr' and COD_IDENT_SALAX in
    (SELECT COD_IDENT_SALAX, max(DAT_IDENT_REUNI) 
      FROM tbl_REUNIOES WHERE COD_IDENT_EMPRE = 'ibhr' GROUP BY (COD_IDENT_SALAX))
  • Renan, could you add more details about your question? The other tables, which relations and etc. It is easier to help you. Basically you want an IN of distinct values, which could be solved with SELECT DISTINCT COD_IDENT FROM tbl_IDENTIFICACAO WHERE FLG_STATUS = 'S' , but post more information, to facilitate in aid. DISTINCT

  • @Fernandoa.W. What I need is all in one table, because next in this table there are several MEETINGS, and EACH MEETING IS A ROOM need to pick up the most recent meeting of each room. Suppose we have 4 rooms, and each room had 10 meetings, I need to pick up the extra with the longer date.

  • I updated the question

  • See if it fits your problem.

1 answer

2


Renan, his IN will not work because you are passing more than one parameter to it, see the documentation IN.

If I understand correctly, you can solve your problem with a SubQuery, in this way:

SELECT   A.COD_IDENT_REUNI, B.MAX_COD_IDENT_REUNI
  FROM   TBL_REUNIOES A, (  SELECT   COD_IDENT_SALAX, MAX (DAT_IDENT_REUNI) MAX_COD_IDENT_REUNI
                              FROM   TBL_REUNIOES
                             WHERE   COD_IDENT_EMPRE = 'ibhr'
                          GROUP BY   (COD_IDENT_SALAX)) B
 WHERE   A.COD_IDENT_REUNI = B.MAX_COD_IDENT_REUNI AND A.COD_IDENT_EMPRE = 'ibhr'

There are other means, but this way meets your problem.

  • He hasn’t returned what I need, in fact he hasn’t returned anything.

  • Renan, post an example of your table, so I can simulate here.

  • Ops actually only your SELECT from within solved my problem

  • Perfect.... =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.