tags:

views:

66

answers:

2
+1  Q: 

mysql query

Hello, in mysql, how do you count the non empty cells in one row? And I want to count the cells between certain columns only, say between columns 3-10. not all the columns... again, in that row only.

+1  A: 

Columns can only be approached in sql with names not numbers if you want to do this you will have to do it in a programming language which is calling the query

Mischa Kroon
A: 

Theoretically speaking, attributes of a relvar are unordered and therefore you should not be able to do this. However, in MySQL I believe you can query the catalogue to obtain the names and also the 'order' of columns of a table. You can get the column names and order like so:

select column_name, ordinal_position from information_schema.columns where table_name='my_table';

However, I doubt this will help you too much. What you're doing smells like bad database design and if you can't do anything about the design, which unfortunately sometimes happens, you can fetch the rows from your table in a programming language, e.g. PHP, and then use loops to 'manually' count the non-empty cells in whatever cells you like. You can fetch the rows into an array and then access individual cells by providing the array index.

If you're happy doing it this way, then why not? It should definitely not be too difficult.

Peter Perháč