Select records under multiple conditions

Asked

Viewed 35 times

0

Code help for selecting in Mysql. Conditions are:

  1. for same 'net' and 'time' records, select the first record (min id) and another record if 'AP' is different
  2. for records of the same time and different net, the above condition is adopted for each group of the same net'

Example

for time 00:13:56 select records 38 and 40 (net= 11) for time 00:13:56 select records 42 and 43 (net= 80) for time 07:30:21 select records 46 and 48 (net= 11) for time 07:30:21 select record 50 (net= 30)

Table data

id  net   hora     AP
38  11  00:13:56    4
39  11  00:13:56    4
40  11  00:13:56    1
41  11  00:13:56    4
42  80  00:13:56    5
43  80  00:13:56    2
44  80  00:13:56    5
45  80  00:13:56    5
46  11  07:30:21    4
47  11  07:30:21    4
48  11  07:30:21    3
49  11  07:30:21    4
50  30  07:30:21    1

1 answer

0


Use the GROUP BY to group records by fields net, hora and AP:

SELECT MIN(id) FROM tabela GROUP BY net, hora, AP
  • Roberto, thank you so much! I’m still learning to use Mysql, I didn’t know that group by could be used to group multiple records at once. Your tip solved my problem!

Browser other questions tagged

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