Selecting a single row in MySQL

后端 未结 3 1226
名媛妹妹
名媛妹妹 2020-12-30 01:03

I am using MySQL, I have a table that has 9 columns. One of them is the primary key.

How can I select a single row, by the primary key or column 8 or 4?

相关标签:
3条回答
  • 2020-12-30 01:29
    select *
    from MyTable
    where MyPrimaryKey = 123
    
    0 讨论(0)
  • 2020-12-30 01:35

    If I understand your question correctly:

    SELECT * FROM tbl WHERE id = 123 OR colname4 = 'x' OR colname8 = 'y' LIMIT 1
    

    The 'LIMIT' keyword makes sure there is only one row returned.

    0 讨论(0)
  • 2020-12-30 01:40

    Columns in SQL don't have a defined 'order'. Database systems generally keep track of an order for display purposes, but it doesn't make sense to ask a database to select a column by number. You need to know the column's name in order to query its contents.

    The same thing goes for the primary key (which, incidentally, may not be just a single column). You have to know which column it is, and what that column is named, in order to execute a query.

    If you don't know these things, or need to figure them out dynamically, then

    DESCRIBE tablename;
    

    will tell you the names of each column, and whether it is part of the primary key or not. It will return a table that you can read, like any other result.

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