DB: saving user height and weight

How do I store the height and weight of a user in a MySQL database so that I can use this information to search for users with a specific height or weight? In addition, I will need to display this information in the English or metric system.

My idea is to store height information in centimeters and weight in kilograms (I prefer the English metric). I can even allow the user to enter their information and the English system, but before saving, do the conversion to metric. I think converting kilograms to kilograms can be easily done in SQL, but I'm not sure how easy it would be to convert 178centimeters to 5'10"(rounded down a bit).

Do I have to store English and metric values ​​in a database so that I don’t need to do conversions when performing my queries? It sounds like a bad idea to store derived / calculated values.

+5
source share
4 answers

There are several ways: you just need to have two numeric columns, one for height, one for weight, and then do the conversion (if necessary) during the display. Another is to create a table “height” and a table “weight”, each of which has a primary key that is associated with another table. Then you can store both English and metric values ​​in these tables (along with any other meta-information you want):

CREATE TABLE height (
    id          SERIAL PRIMARY KEY,
    english     VARCHAR,
    inches      INT,
    cm          INT,
    hands       INT  // As in, the height of a horse
);

INSERT INTO height VALUES
    (1,'4 feet',           48, 122, 12),
    (2,'4 feet, 1 inch',   49, 124, 12),
    (3,'4 feet, 2 inches', 50, 127, 12),
    (3,'4 feet, 3 inches', 51, 130, 12),
    ....

...

- , , - , ..

CREATE TABLE users (
    uid         SERIAL PRIMARY KEY,
    height      INT REFERENCES height(id),
    weight      INT references weight(id),
    sign        INT references sign(id),
    ...
);

4 5 :

SELECT *
FROM users
JOIN height ON users.height = height.id
WHERE height.inches >= 48 AND height.inches <= 60;

:

  • "" ( - ) - , !
  • HTML select super easy - SELECT english FROM height ORDER BY inches, .
  • - (, ), , - .
  • (, "" )
+5

, , , 178 5'10 ", 70", , , 5'10 ".

+3

5'10 " 70" 5.8333333 '. 70 5.83333 , db , .

, , .

+1

, . .

- , , , /, , .

- :

, 100% - 90%.

, - .

, - , DB

0
source

All Articles