2

I'll try to explain you the problem of mine. I'm searching for advice because I'm in impasse. I have this database for an online store (it doesn't sell anything, it's just to show people owner's products).

I have two (they are more, but for this situation we'll consider just two of them) table. In the first:

products name | gender | id_product | etc..

    john        male     23

In the second:

id_product | man_56 | man_54 | man_52 | man_50 | etc...

 23         0         1       0        1

id_product is the same as the first table. It's used to link the two table. Also 1=available, 0=not available).

Screenshot:

enter image description here

Now I don't know how to select/print only the column where records are 1 and not 0. Do I need to specify every column's name of the table?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Narheru
  • 45
  • 4
  • You need to explain this question a lot more. For a start, showing more than one row of data, plus your desired outcome would help – Steve Nov 16 '15 at 19:28
  • Ok. I've taken a screen of my table to show you how it's thought: http://i63.tinypic.com/15i59h4.png The result should be(it should print only where column_name=1): For id_product 15 Avaliable size: Woman: 40, 42 For id_product=12 Avaliable size: Children 7, 9 – Narheru Nov 16 '15 at 19:36
  • Looks like you comment got cut - please edit your question with the details – Steve Nov 16 '15 at 19:39
  • Yes, sorry. I've completed it right now – Narheru Nov 16 '15 at 19:39
  • Well the proper way to do it would be to normalize your database. However with that data structure you would be best off just selecting the whole row, then checking if each value == 1 before printing – Steve Nov 16 '15 at 19:48
  • What do you mean "normalize"? Thanks for the advice – Narheru Nov 16 '15 at 22:51
  • Well, thats a big subject you would need to google, but in this case, it means breaking your problem down into 3 tables instead of two: your products table, as it is, a 'productAttributes' table that contains 2 columns : attributeID and description, (eg: `1 | men_42`,`2 | men44` etc) and 3rd table: `products_attributes` that contains a link between the two tables with columns productID and attributeID columns. – Steve Nov 16 '15 at 23:03
  • Thanks, I'll google and then I'll improve it. Thanks :D – Narheru Nov 23 '15 at 11:45

3 Answers3

1

You can use

SELECT * from tablename where columname = 1

tablename is your name of table i.e products columnname is the name of feild which you want to check.

0
SELECT * FROM TableName WHERE 1 IN (ColumnOne,ColumnTwo,ColumnThree...)

For more info, check this link

Community
  • 1
  • 1
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
0

I've solved my problem using multiple "if". Thanks to all of you :D

Narheru
  • 45
  • 4