tags:

views:

76

answers:

4

Hello all

I have a table in a MySQL database. I am given a value that occurs as a cell value in that table but I do not know which cell is it i.e. the row and column of that cell. What is the most efficient way to find the column to which that value belongs? Thanks in advance.

Example:

Column_1 | Column_2 | Column_3
1        | 2        | 3
4        | 5        | 6
7        | 8        | 9

Now I am given an input value of "8". I want to know if there is an efficient way to find out that value of "8" belongs to Column_2.

+1  A: 

It's a bit strange that you don't know which column the data is in, since columns are meant to have a well-defined function.

[Original response scrubbed.]

EDIT: Your updated post just asks for the column. In that case, you don't need the view, and can just run this query

SELECT col FROM (
   SELECT "Column_1" AS col, Column_1 AS value FROM YourTable
   UNION ALL SELECT "Column_2", Column_2 FROM YourTable
   UNION ALL SELECT "Column_3", Column_3 FROM YourTable
) allValues
WHERE value=8;

When you run this query against your table, it will return "Column_2"

mdma
Hi, please see my updated post.
mdma
Wow, your solution seems to be really good. It worked easily with my first table. Let me try it with other tables, and I will get back to you. Thanks!
shuby_rocks
The reason that I do not know about the functions among the columns is that the tables are created manually by a user in a blank table. The user does not specify the functions, s/he just writes the data in the table and saves it.
shuby_rocks
I understand. So there is no real logical relationship between the columns and the data in them? If that's the case, and you would prefer having the data in one column, you can use my query to produce a dataset with a single column, and insert the data as a new table. Check out SELECT INTO syntax in the MySQL manual. (Or INSERT ... FROM SELECT - the first creates a new table, the second inserts data into an existing table.)
mdma
There is supposed to be a logical relationship between the columns and data stored in them but we do not know about them. It is a part of our research project to infer such logical relationships. That's the whole story :)
shuby_rocks
Aha! Sounds like you have engaging work ahead - Good luck with that!
mdma
A: 

You might be able to adapt this for your needs. (If it is always a number you are searching for then excluding all character columns will make it more efficient)

Martin Smith
A: 

Without knowing more about your app, you have several options:

  • Use MySQL's built-in full-text search. You can check the MATCH function in the MySQL documentation.
  • Depending on the needs of your app you could decide to index your whole table with an external full-text search index, like Solr or Sphynx. This provides instant response time, but you'll need to keep the index updated.
  • You can loop through all the columns in the table doing a LIKE query in MySQL (very expensive in CPU and time)
I am not sure how can I use the match function to get the exact column to which it belongs.
shuby_rocks
A: 

You're designing this table with repeating groups, which is not satisfying First Normal Form.

You should create a second table and store the values for column1, column2, and column2 in a single column, on three rows.

Learn about the rules of database normalization for more details.

Bill Karwin
The reason that I do not have normalized tables is that the tables are created manually by a user writing data in cells of a blank table. The user does not specify the functions, s/he just writes the data in the table and saves it.It is a part of our research project where we then try to infer functions that may exist between column and normalize the tables.
shuby_rocks
I can predict the conclusion of your research: *let user input supply data, but not code* (DDL counts as code).
Bill Karwin