Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select specific rows and columns from an SQL database

Tags:

sql

mysql

Is it possible to retrieve specific columns of specific rows in an SQL query?

Let's say I'm selecting from my SQL table, called my_table, rows whose names are: a, b, using this query text:

"select * from my_table where row_names in ('a', 'b') order by row_names"

How can I modify this query text to select only columns 2,12,22,32,42 rather all its 1000 columns?

like image 696
dan Avatar asked Apr 17 '16 22:04

dan


People also ask

How do I select only certain rows in SQL?

Selection symbols to narrow row selection To select rows using selection symbols for character or graphic data, use the LIKE keyword in a WHERE clause, and the underscore and percent sign as selection symbols.


2 Answers

Replace the wildcard symbol * with the column names you want to retrieve.

But please read up the documentation on SQL standard. It is very unlikely you need 1.000 columns in a table.

like image 197
Anthony Drogon Avatar answered Sep 28 '22 09:09

Anthony Drogon


Try and read the sql, as it really does what it says

select [column name 1, column name 2] 
from [table name] 
where [column name 3] in ('column value 1', 'column value 2')
order by [column name 3] 

please select the values of "column name 1" and "column name 2" from rows in the table called "table name" where those rows have values equal to 'column value 1' and 'column value 2' in the column called "column name 3"

like image 21
Ultradiv Avatar answered Sep 28 '22 08:09

Ultradiv