3

I am currently using the SQL command

Select * from where name='john'

Is it possible to return 20 no matter the query, for example

Select * from where name='john' or return = 20
Tom
  • 9,725
  • 3
  • 31
  • 48
gaurav singh
  • 1,376
  • 1
  • 15
  • 25
  • 2
    What are you expecting as your results? You're using `*` to select all columns but want to return just 20? If you want to return 20, you could just use `SELECT 20` But I feel that's not what you want. Also, [have a look at this](https://stackoverflow.com/questions/2679865/return-a-value-if-no-rows-are-found-sql) – Tom Jun 01 '17 at 10:14
  • Possible duplicate of [Return a default value if no rows found](https://stackoverflow.com/questions/15319264/return-a-default-value-if-no-rows-found) – Tom Jun 01 '17 at 10:15
  • @Tom if i run this commands Select * from abc where id=88 or (select 20) limit 1 it gives me one row and all column result . but here i want only to return 20 .without changing Select * from abc where id= – gaurav singh Jun 01 '17 at 12:17

3 Answers3

2

EDIT If you have an oracle database you can do something like that:

SELECT * 
FROM dual
WHERE 1=0
UNION
SELECT '20'
FROM dual;
scatolone
  • 733
  • 1
  • 5
  • 21
1

check my answer

 if exists (Select * from item where ItemName='ABC Daycare1')
  begin
  Select * from item where ItemName='ABC Daycare1'
  end
  else
  select '20'
Jinto John
  • 365
  • 4
  • 22
  • there is one condition i can't change this part Select * from abc where name= ...i can only add changes after "Select * from where name" – gaurav singh Jun 01 '17 at 10:48
  • @gauravsingh...I wont get u man...........plz let me know correct scnario.then only I can provide exact answer.. – Jinto John Jun 01 '17 at 10:50
  • Yes it return 20 but i want query like this select * from abc where name ='jinto' or select 1000 , it gives all the table data but here cant manipulate this select * from abc wherd name= this should be fixed , manipulation only can be done after select * from abc where name= and should return 20 – gaurav singh Jun 01 '17 at 11:31
  • Just like sql injection – gaurav singh Jun 01 '17 at 11:33
  • if i run this commands Select * from abc where id=88 or (select 20) limit 1 it gives me one row and all column result . but here i want only to return 20 .without changing Select * from abc where id= – gaurav singh Jun 01 '17 at 12:00
0

Try running this. This should return the top result (which is never 20 due to the custom sort) and then when the name doesn't match a value it returns 'Mark' and 20

SQL

IF OBJECT_ID('tempdb..#temp') IS NOT NULL DROP TABLE #temp

CREATE TABLE #temp (id int NOT NULL, name varchar(255) NOT NULL)

INSERT INTO #temp (id, name) VALUES (88,'John')
INSERT INTO #temp (id, name) VALUES (20,'Mark')

SELECT TOP 1 
    * 
FROM #temp 
WHERE (name = 'Mark' OR name = 'John') 
ORDER BY (
    CASE
        WHEN id = 20 THEN 0 ELSE 1
    END) DESC

MySQL - MySQL fiddle

IF OBJECT_ID('tempdb..#temp') IS NOT NULL DROP TABLE #temp

CREATE TABLE #temp (id int NOT NULL, name varchar(255) NOT NULL)

INSERT INTO #temp (id, name) VALUES (88,'John')
INSERT INTO #temp (id, name) VALUES (20,'Mark')

SELECT 
    * 
FROM temp 
WHERE (name = 'Mark' OR name = 'John') 
ORDER BY (
    CASE
        WHEN id = 20 THEN 0 ELSE 1
    END) DESC
LIMIT 1
Tom
  • 9,725
  • 3
  • 31
  • 48