Sadbot
Sadbot

Reputation: 1

Select values from the same column

I have a database related question for WordPress.

My table is represented as follows:

ID | customID | column1 | column2

1      8      _item    item number

2      8      _price    item price

3      9      _item    item number

4      9      _price    item price

I want to retrieve the following rows using a MySQL SELECT statement:

CustomID | _price | _item |

8       item price(from col2)    item number(from col2)
9       item price(from col2)    item number(from col2)

Is this possible? The value _price and _item should be shown as columns with values from column2. How to solve this?

Upvotes: 0

Views: 36

Answers (1)

Sebastian Lerch
Sebastian Lerch

Reputation: 424

Your tables are designed quite badly. You should build an extra table which maps product and price. However this is what you can do with your solution:

Select t1.customID, t1.column2, t2.column2 from <tablename> t1,
 <tablename> t2 
 where t1.customID = t2.customId 
 and t1.column1 like '_item' 
 and t2.column1 like '_price';

Upvotes: 1

Related Questions