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
3
  • 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 Commented Jun 1, 2017 at 10:14
  • Possible duplicate of Return a default value if no rows found Commented Jun 1, 2017 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= Commented Jun 1, 2017 at 12:17

3 Answers 3

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;
Sign up to request clarification or add additional context in comments.

2 Comments

there is one condition i can't change this part Select * from abc where name= ...i can only add changes after "Select * from abc where name"
m getting error " The used SELECT statements have a different number of columns"
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'

5 Comments

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"
@gauravsingh...I wont get u man...........plz let me know correct scnario.then only I can provide exact answer..
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
Just like sql injection
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=
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.