Jahangir Alam
Jahangir Alam

Reputation: 49

select query to limit no. of columns and rows mysql

I have a MySQL table named city_map. I want to display only the given fields in the snapshot in red rectangale.

Suppose the sl_no is given as 1 to 4, how do I get the required output?

Screenshot of table output

I am trying with this query:

select * from city_map where sl_no between 1 and 4

And I'm getting the first 4 rows but I only want to display the columns Hyderabad, Itanagar, Dispur and Patna.

Upvotes: 0

Views: 102

Answers (3)

ravikumar
ravikumar

Reputation: 893

Use the variable names in the query and pass the value from method as query parameter,

SELECT sl_no,capital,Hyderabad,Itanagar,Dispur,Patna 
FROM city_map WHERE sl_no BETWEEN :slNoFrom AND :slNoTo ORDER BY sl_no

try using String.format as,

String.format(yourQuery,
 NEW Object[]{ColumnName1,ColumnName2,ColumnName3,ColumnName4,ColumnName5}

and use %s as a placeholder in a query,

 SELECT sl_no,%s,%s,%s,%s,%s
 FROM city_map WHERE sl_no BETWEEN :slNoFrom AND :slNoTo ORDER BY sl_no

Upvotes: 0

Aman Aggarwal
Aman Aggarwal

Reputation: 18459

Use the simple query :

select sl_no,capital,Hyderabad,Itanagar,Dispur,Patna from city_map where sl_no between 1 and 4

Upvotes: 0

try this :

select sl_no,capital,Hyderabad,Itanagar,Dispur,Patna from city_map where sl_no between 1 and 4

Upvotes: 2

Related Questions