Make selection from the results obtained from it

Asked

Viewed 79 times

1

Good afternoon,

I have the following table..

*eventos*
----------------------------------------
| id_evento | id_projeto | nome_evento |
----------------------------------------

I need to make a process as follows:

"Select events where the id_project is 1 with a limit of 2 records, but if the number of records is less than 2, select the last recorded events to complete the total number of 2 records."

Detailing... I need to select the records that have determined id_projeto but if by chance there is no record(with this id) I need to select the last two of the table events, and if by chance there is only 1, then I need to select only 1 more record.

I hope I’ve been clear. Any questions I answer.

Obs.: I am using mysql database

  • The gist of the matter is greater than the question ue q q isso kkkk

  • @Francisco kkkkk truth ^^

2 answers

3


Use the ORDER BY FIELD next to the LIMIT to do this:

SELECT * FROM eventos ORDER BY FIELD(id_projeto, 1) DESC, id_evento DESC LIMIT 2
  • but then if you have more than two events with id_project 1, they will not be shown

  • @Rovannlinhalis But that’s exactly what the question is asking.

  • I think you actually have to show at least two records, if you have more that matches the id_project...it has to be shown too

  • @Exact Francis.. But if by chance there is only one record, it will pull another with another id, right?

  • @Rovannlinhalis actually I only need 2 even

  • @Andreicoelho Yes. You can test.

  • @Francis thank you!

  • @Rovannlinhalis I don’t know your concept of limit, but I think you got the question wrong.

  • "but if by chance there is no record with this id, I need to select the last 2" but blz, I understood otherwise...

  • guy...sure this works? http://sqlfiddle.com/#! 9/df682a/1

  • @Rovann Linhalis not yet tested...

  • the Field function will return the index of the parameter within the field, that is, it will bring those who do not have 1 always in front, and then those who have another problem, having id_project = 1 or =10, the ordering of the two becomes equal: https://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_field

  • 1

    @Rovannlinhalis I think you got the FIELD function a little wrong, it doesn’t see if there is the value in front, it tries to take exactly that value. I had forgotten about DESC, now it’s working perfectly: http://sqlfiddle.com/#! 9/df682a/12

  • Really, sorry. @Andreicoelho Francisco’s syntax will look much better for your need =]

  • So, but in this case http://sqlfiddle.com/#! 9/df682a/24 he should take event 9 as well, but he’s taking event 2. So I still think the @Rovannlinhalis response is better for now.

  • 1

    @Andreicoelho Just add another order by, I edited my answer, take a look.

  • @Rovannlinhalis has nothing to apologize for, nor do I know how this function works right, I used it only 1 time in life kk

  • Cool! Thank you all! Francisco and @Rovannlinhalis

Show 13 more comments

3

Considering the consultation by id_projeto = 1 can be done this way:

select 
id_evento,id_projeto,nome_evento, 1 as prioridade
from eventos where id_projeto =1

union

select 
id_evento,id_projeto,nome_evento, 2 as prioridade
from eventos where id_projeto !=1

order by prioridade, id_evento desc
limit 2;

I put in Sqlfiddle: http://sqlfiddle.com/#! 9/df682a/4

  • 1

    Perfect! That’s exactly what it was!

  • =] was trying to resolve the other way that had understood the question

  • 1

    =) Anyway, thank you so much! I was going to hit the wall! rsrsrs

Browser other questions tagged

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