![]() | |
#11
| |||
| |||
|
|
On 1/2/2011 4:31 PM, bruce wrote: On Jan 2, 3:36 pm, Jerry Stuckle<jstuck... (AT) attglobal (DOT) net> *wrote: On 1/2/2011 3:17 PM, Luuk wrote: On 02-01-11 19:50, Jerry Stuckle wrote: On 1/2/2011 10:34 AM, bruce wrote: Is there anyway, using a SELECT statement, I can determine what the PRIMARY KEY is? I want the select a field from the last row in a database. I'm using a select statement SELECT myfield FROM table WHERE primarykey - (SELECT MAX(primarykey) FROM table) Or is there a better way to get a field from the last row? Thanks.. Bruce Define "last row"? Tables are by definition unordered. The only way you can get a "last" (or "first") row is to ORDER BY a field. But, if you read the original message: "WHERE primarykey - (SELECT MAX(primarykey) FROM table)" This will ALWAYS produce just 1 record, so what order are you talking about? That will get the row with the highest primary key, true. *But that is not necessarily the last row. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstuck... (AT) attglobal (DOT) net ================== Sorry. I should have said I wanted the "Highest" primary key... So, is there some way I can find out the row with the "Highest" primary key without knowing the name of the primary key. This will give me the primary key name (SHOW INDEX FROM table) but I don't know how to incorporate this information to my statement SELECT myfield FROM table WHERE primarykey - (SELECT MAX(primarykey) FROM table) I also found the following which returns the primary key name, but again I don't know how to incorporate the result into my SELECT. SELECT k.COLUMN_NAME FROM information_schema.table_constraints t LEFT JOIN information_schema.key_column_usage k USING(constraint_name,table_schema,table_name) WHERE t.constraint_type='PRIMARY KEY' * * *AND t.table_schema=DATABASE() * * *AND t.table_name='mytable'; You would have to do a SHOW CREATE TABLE tblname and parse the result. Bear in mind you may have multiple columns in a primary key, and the primary key may or may not be numeric. *And in some tables the concept of "highest primary key" may be nonexistent - as in an n:n link table. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstuck... (AT) attglobal (DOT) net ================== |
#12
| |||
| |||
|
|
On Jan 2, 3:36*pm, Jerry Stuckle <jstuck... (AT) attglobal (DOT) net> wrote: On 1/2/2011 3:17 PM, Luuk wrote: On 02-01-11 19:50, Jerry Stuckle wrote: On 1/2/2011 10:34 AM, bruce wrote: Is there anyway, using a SELECT statement, I can determine what the PRIMARY KEY is? I want the select a field from the last row in a database. I'm using a select statement SELECT myfield FROM table WHERE primarykey - (SELECT MAX(primarykey) FROM table) Or is there a better way to get a field from the last row? Thanks.. Bruce Define "last row"? Tables are by definition unordered. The only way you can get a "last" (or "first") row is to ORDER BY a field. But, if you read the original message: "WHERE primarykey - (SELECT MAX(primarykey) FROM table)" This will ALWAYS produce just 1 record, so what order are you talking about? That will get the row with the highest primary key, true. *But that is not necessarily the last row. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstuck... (AT) attglobal (DOT) net ================== Sorry. I should have said I wanted the "Highest" primary key... So, is there some way I can find out the row with the "Highest" primary key without knowing the name of the primary key. This will give me the primary key name (SHOW INDEX FROM table) but I don't know how to incorporate this information to my statement SELECT myfield FROM table WHERE primarykey - (SELECT MAX(primarykey) FROM table) I also found the following which returns the primary key name, but again I don't know how to incorporate the result into my SELECT. SELECT k.COLUMN_NAME FROM information_schema.table_constraints t LEFT JOIN information_schema.key_column_usage k USING(constraint_name,table_schema,table_name) WHERE t.constraint_type='PRIMARY KEY' * * AND t.table_schema=DATABASE() * * AND t.table_name='mytable'; |
#13
| |||
| |||
|
|
On 1/2/2011 4:31 PM, bruce wrote: On Jan 2, 3:36 pm, Jerry Stuckle<jstuck... (AT) attglobal (DOT) net> *wrote: On 1/2/2011 3:17 PM, Luuk wrote: On 02-01-11 19:50, Jerry Stuckle wrote: On 1/2/2011 10:34 AM, bruce wrote: Is there anyway, using a SELECT statement, I can determine what the PRIMARY KEY is? I want the select a field from the last row in a database. I'm using a select statement SELECT myfield FROM table WHERE primarykey - (SELECT MAX(primarykey) FROM table) Or is there a better way to get a field from the last row? Thanks.. Bruce Define "last row"? Tables are by definition unordered. The only way you can get a "last" (or "first") row is to ORDER BY a field. But, if you read the original message: "WHERE primarykey - (SELECT MAX(primarykey) FROM table)" This will ALWAYS produce just 1 record, so what order are you talking about? That will get the row with the highest primary key, true. *But that is not necessarily the last row. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstuck... (AT) attglobal (DOT) net ================== Sorry. I should have said I wanted the "Highest" primary key... So, is there some way I can find out the row with the "Highest" primary key without knowing the name of the primary key. This will give me the primary key name (SHOW INDEX FROM table) but I don't know how to incorporate this information to my statement SELECT myfield FROM table WHERE primarykey - (SELECT MAX(primarykey) FROM table) I also found the following which returns the primary key name, but again I don't know how to incorporate the result into my SELECT. SELECT k.COLUMN_NAME FROM information_schema.table_constraints t LEFT JOIN information_schema.key_column_usage k USING(constraint_name,table_schema,table_name) WHERE t.constraint_type='PRIMARY KEY' * * *AND t.table_schema=DATABASE() * * *AND t.table_name='mytable'; You would have to do a SHOW CREATE TABLE tblname and parse the result. Bear in mind you may have multiple columns in a primary key, and the primary key may or may not be numeric. *And in some tables the concept of "highest primary key" may be nonexistent - as in an n:n link table. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstuck... (AT) attglobal (DOT) net ================== |
#14
| ||||
| ||||
|
|
Is there anyway, using a SELECT statement, I can determine what the PRIMARY KEY is? |
|
I want the select a field from the last row in a database. I'm using a |
|
select statement SELECT myfield FROM table WHERE primarykey - (SELECT MAX(primarykey) FROM table) |
|
Or is there a better way to get a field from the last row? |
#15
| |||
| |||
|
|
The OP could learn to use google... however, Wouldn't it be easier to query the INFORMATION_SCHEMA? From: http://rpbouman.blogspot.com/2008/03...entifying.html 'SELECT k.column_name FROM information_schema.table_constraints t JOIN information_schema.key_column_usage k USING(constraint_name,table_schema,table_name) WHERE t.constraint_type='PRIMARY KEY' AND t.table_schema='db_name' AND t.table_name='tbl_name';' |
#16
| |||
| |||
|
|
El 03/01/2011 23:07, onedbguru escribi /wrote: The OP could learn to use google... however, Wouldn't it *be easier to query the INFORMATION_SCHEMA? From: *http://rpbouman.blogspot.com/2008/03...chema-identify... 'SELECT k.column_name FROM information_schema.table_constraints t JOIN information_schema.key_column_usage k USING(constraint_name,table_schema,table_name) WHERE t.constraint_type='PRIMARY KEY' AND t.table_schema='db_name' AND t.table_name='tbl_name';' In shared hosting services it's not common to have permissions over the information_schema database. -- --http://alvaro.es- lvaro G. Vicario - Burgos, Spain -- Mi sitio sobre programaci n web:http://borrame.com -- Mi web de humor satinado:http://www.demogracia.com -- |
![]() |
| Thread Tools | |
| Display Modes | |
| |