简体   繁体   中英

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?

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';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM