0
I have the following table PLAYLIST
where I have the columns VIDEO
and STATUS
:
VIDEO | STATUS
The column status
varies between 1
and 0
being 1
for new videos and 0
for videos that have already been watched.
I’m currently performing two select
:
SELECT * FROM playlist WHERE status='1';
if result > 0 {
echo ok
} else {
SELECT * FROM playlist WHERE status='0'
}
You can make this query using only in 1 SELECT
? preferred without using php?
My goal is to prioritize videos that have not been viewed.
Just take the
where
doesn’t solve? has some problem?– rray
But he wants the remaining result, where the status is 0. It is not ?
– rbz
and much like if php so that wanted to sql ,
– Arsom Nolasco
You want to take the data from the playlist table that the status is 1 and if the result is empty (no line) grab all the status is 0?
– Costamilam
I believe that
status
can only have the values0
and1
; so you really wouldn’t need thewhere
, as @rray said.– rLinhares
@Guilhermecostamilam yes exactly that
– Arsom Nolasco
the user when posting the video it already goes with the status=1 , when watching the update video in status and marks the video as 0 = already displayed , and select is so that if all the videos have already been shown it will run those that have status=0
– Arsom Nolasco
I don’t know what the purpose of the code is but one option is
count()
withgroup by
the return has the columnstatus
and how often (total
) he appeared.– rray
Why not just use the last query then?
SELECT * FROM playlist WHERE status='0'
– rLinhares
@rLinhares rray purpose in the above comment, basically I want to prioritize the new videos that was inserted
– Arsom Nolasco
If you just want to prioritize can use
ORDER BY status DESC
, so those with status 1 will come first, but if there can’t appear those with status 0 if there are any with status 1, take a look at my answer, I believe that’s what you’re looking for– Costamilam
He killed the puzzle by the status will give right in 1 query, now only tells me how to sort by ID and STATUS at the same time ORDER BY ID.STATUS DESC?
– Arsom Nolasco
Use
ORDER BY status DESC
it will sort by status and then by id, because in the bank the natural order is by id, but if you want you can useORDER BY status DESC, id
– Costamilam
@Guilhermecostamilam put there as an answer for me to score
– Arsom Nolasco