Select specific rows and columns from an SQL database

前端 未结 4 1242
余生分开走
余生分开走 2020-12-10 18:59

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_

相关标签:
4条回答
  • 2020-12-10 19:06

    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"

    0 讨论(0)
  • 2020-12-10 19:09

    2 imp points
    1. 1000 columns in single table is against RDBMS and its highly unnormalised table.
    2. rows doesnot have name.

    Now if you know names of columns then use following:

    "select column_1,column_2 from my_table where primary_key_column in ('1','2');"
    

    here column_1,column_2 are names of columns you want.
    primary_key_column to uniquely define your rows and in brackets all the primary keys you need to retrieve.

    If you know only know column numbers then use following to retrieve column names:

    "SELECT Column_name FROM user_tab_columns where table_name = 'MY_TABLE' and Column_id in (1,2,5);"
    

    Hope this will help.

    For further reference http://www.w3schools.com/sql/

    0 讨论(0)
  • 2020-12-10 19:24

    Yes. SQL is used to select required columns from required rows. The data rows don't have row_names unless you've defined a column with the name row_names (this is not done as a rule). At a basic level, retrieving data from an RDBMS is through an SQL statement that is constructed as follows: (This'd be a SELECT statement. The simplest version has the following clauses)

    1. Pick the table which holds the data of interest (FROM clause). If we want telephone listings, we might query the whitepages table.
    2. Identify the rows which hold the data of interest (WHERE clause). This is done by putting conditions on column values (there are no row hearders or row names). We want phone numbers for people whose last name is "Hemingway" Here the last name would be a column in the table, not the row name.
    3. Pick the data columns we want to retrieve (SELECT clause). We want the first name, last name and phone number.

    Putting it all together, we might have:

    SELECT
        first_name,
        last_name,
        phone_number
    FROM
        WhitePages
    WHERE
        (last_name = "Hemingway")
    

    In your case, the SELECT statement would be something like:

    SELECT
        <name of column 2>,
        <name of column 12>,
        <name of column 22>,
        <name of column 32>,
        <name of column 42>
    FROM
        my_table
    WHERE
        (<some_column_name> IN ('a', 'b'))
    AND (<some_other_column_name> = <some_number>)
    AND (<yet_another_column_name> = "<some_text>")
    ORDER BY
        <a_selected_column>,
        <another_selected_column>
    

    If you're going to work with databases on a regular basis, I'd recommend learning a bit more about SQL as you'll have a difficult time getting by with questions on SO or other web sites.

    While the 1000 columns you mention might be hyperbole, SQL tables do not normally have hundreds of columns. Consider having your tables and SQL designed by an experienced team member.

    0 讨论(0)
  • 2020-12-10 19:26

    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.

    0 讨论(0)
提交回复
热议问题