mysql - Add a dummy row when the query returns at least one result -
i have seen few sql questions regarding "how add row when none exists" or "how return dummy row when no result found", can't figure out way opposite result. objective here add row whenever there's @ least 1 result found. however, when no result found, nothing should returned.
given following users
table:
id username ------------------ 1 bob 2 alice
and dummy row (which isn't stored in db) :
id username ------------------ 0 dummy
here result i'm trying get:
# query: find users id 1. id username ------------------ 1 bob 0 dummy # query: find users id 3. (empty set) # query: find users id 0. (empty set)
of course, dummy row added once (and not once per actual record). now, working following query...
select * ( select * users id = 1 union select 0, 'dummy' )
yet, adds dummy row in cases, when first sub-select
returns empty set. there way add row when necessary?
note: mysql database.
try this:
select id, username users id = 1 union select * (select 0 id, 'dummy' username) t exists (select 1 users id = 1);
Comments
Post a Comment