Home > Enterprise >  How to return all row values for any given particular column in MYSQL Database table - meant to be u
How to return all row values for any given particular column in MYSQL Database table - meant to be u

Time:12-28

I am working with a JDBC and am trying to figure out how to dynamically create queries - which is proving to be a rather challenging feat. What input can I put in a SQL query to have it return any input for a given column?

For example,

SELECT * FROM customers WHERE name = ?;

Essentially I want the previous statement to match

SELECT * FROM customers;

Instead of replacing the "Where name = ?", is there anything I can replace the ? with to get the database to return all rows with any value in the name column?

This is probably going to sound really stupid but I tried this,

SELECT * FROM customers WHERE name = *;

but that unfortunately did not work.

CodePudding user response:

There is at least one solution:

SELECT * FROM customers WHERE name = coalesce(?, name);

However this method disables index search when ? is not null

EDIT: use coalesce instead of Oracle-specific nvl

EDIT2: this solution does NOT work for null values

Another option is just use second parameter as marker for "gimme more"

SELECT * FROM customers WHERE name = ? or ? = 1;

CodePudding user response:

Ok, so I think I figured it out from another post. Essentially just call

SELECT * FROM customers WHERE name = name OR name IS NULL;

This worked for me.

  • Related