Polar Bear
Polar Bear

Reputation: 131

How to get column name according to value? impossible?

Recently im stuck with this code...

I would like to know the column_name that matches the value...

For Example:

I have a table called favorite with userID and 5 favorite columns which stores restaurant's ID as follows:

mySQL table

I printed them into a listview and when i delete them, i get the restaurantID and then search for the columns that equals to the value, then update the column into NULL.

Sorry if my question confused you... I myself is confusing.

so how should i do the SELECT statement? or i shall straight away UPDATE?
like

SELECT $column from favorite WHERE loginID= 'admin' AND $column = '99'?

or

UPDATE favorite SET $column = 'NULL' WHERE loginID = 'admin'?   

what should the $column be? >.<

Upvotes: 0

Views: 63

Answers (1)

CodeZombie
CodeZombie

Reputation: 5377

You should really read about database normalization and create a proper database schema like:

Table login:

loginID | name
      1 |  foo
      2 |  bar

Table login_favorite:

loginID | favoriteID
      1 |          1
      1 |          2
      2 |          2

Table favorite

favoriteID | name | ...
         1 |  foo | ...
         2 |  bar | ...

Now JOIN these tables and enjoy the feeling of having a clean database...

Upvotes: 2

Related Questions