StackOverflowNewbie
StackOverflowNewbie

Reputation: 40653

Database Design: dating-site-like application?

Assume a dating-site-like web application wherein each profile can have one or more users. Each user, say, has the following columns: name, age, gender.

I think the DB design would be something like the following:

TABLE profile
=============================
| profile_id | name         |
=============================
| 1          | Dynamic Duo  |
-----------------------------

TABLE user
================================================
| user_id | profile_id | name   | age | gender |
================================================
| 1       | 1          | Batman | 35  | Male   |
------------------------------------------------
| 2       | 1          | Robin  | 25  | Male   |
------------------------------------------------

Is the DB design good? How would I query for the following:

If the DB design can't support such queries, what design would?

Upvotes: 1

Views: 2398

Answers (1)

k_b
k_b

Reputation: 2490

I gave the gender attribute the type of bit.

The first one would be;

-- All male
SELECT * FROM profiles p
  WHERE p.id NOT IN
    (SELECT u.profile_id FROM users u WHERE u.gender != 1);

Second;

-- Male and female
SELECT * FROM profiles p
  WHERE p.id IN
    (SELECT u.profile_id FROM users u WHERE u.gender = 1)
    AND p.Id IN
    (SELECT u.profile_id FROM users u WHERE u.gender = 0)

Third is similar, just add AND u.age > 20 AND u.age < 40 to both subqueries.

Fourth;

-- Number of associated users above 2
SELECT *, count(p.id) AS user_count FROM profiles p
  JOIN users u ON p.id = u.profile_id
  GROUP BY p.id HAVING count(u.id) > 2

Upvotes: 1

Related Questions