0

I'm developing a sort of social network for my school.

There are two tables in the database there are two tables:

  • users, which contains (varchar) username and (json) friends—which contains a list of usernames that the user is friends with—and
  • posts, which contains the (varchar) publisher field.

How can I write a query which returns all posts where the publisher is friends with a given user?

enter image description here

Post example:

| id  | publisher  | originalPublisher | postdate   | content                                                                       | likes |
| 113 | pvaqueiroz | NULL              | 2017-03-13 | {"contentType":"text","content":"DAB \\o\\","attatchments":[]} |     0 |

User example:

| id | username   | password                         | email                | fullname      | likes                                         | friends                                               | profpic                                                 |
|  1 | pvaqueiroz | 827ccb0eea8a706c4c34a16891f84e7b | [email protected] | Paulo Queiroz | [31, 30, 0, 63, 68, 85, 89, 91, 92, 109, 114] | ["hacker", "girassol_l", "Dment", "Leel", "Mr Dibre"] | /carlos/posts_res/d11109ac342482457f87611483d661a6.jpeg |
7
  • Added a picture with both schemas Mar 15, 2017 at 0:03
  • Please, just a sample of your json fields.
    – McNets
    Mar 15, 2017 at 0:05
  • 1
    That is not a php question. What did you try?
    – user5156016
    Mar 15, 2017 at 0:10
  • 1
    Try classic m2m setup. Add friends table that has 2 IDs from user table follower_id and fallowing_id. If you want to store it as json you picked wrong db and structure.
    – E_p
    Mar 15, 2017 at 0:16
  • 1
    "m2m" means "many-to-many", it's a relational database term. There's no reason to go back to MongoDB just because of this little issue. Break out your JSON as first-class columns that can be properly indexed.
    – tadman
    Mar 15, 2017 at 0:27

1 Answer 1

0

So the right solution.

Create statements(skipping fields as they not required for explanation)

CREATE TABLE `users` (
  `id` INT  UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NULL,
  `email` VARCHAR(45) NULL,
  `password` VARCHAR(45) NULL,
  PRIMARY KEY (`id`));

CREATE TABLE `posts` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `post` VARCHAR(45) NULL,
  `user_id` INT UNSIGNED NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_posts_1_idx` (`user_id` ASC),
  CONSTRAINT `fk_posts_1`
    FOREIGN KEY (`user_id`)
    REFERENCES `users` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

CREATE TABLE `friends` (
  `user_id` INT UNSIGNED NULL,
  `friend_id` INT UNSIGNED NULL,
  UNIQUE INDEX `user_friend_idx` (`user_id` ASC, `friend_id` ASC),
  INDEX `fk_friends_f_users_idx` (`friend_id` ASC),
  CONSTRAINT `fk_friends_u_users`
    FOREIGN KEY (`user_id`)
    REFERENCES `users` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_friends_f_users`
    FOREIGN KEY (`friend_id`)
    REFERENCES `users` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

Every time you add a fried to a user you would just record a pair of ids in friends table

And queries will be:

-- Selecting user friends
SELECT * FROM friends f 
JOIN users u ON u.id = f.friend_id
WHERE f.user_id = 1 -- Id of user you need friends for

-- Selecting friends posts
SELECT * FROM friends f 
JOIN posts p ON p.user_id = f.friend_id
WHERE f.user_id = 1 -- Id of user you need friends' posts for
4
  • Thanks a for your answer, it works perfectly. I'm sorry bothering with this stupid question, but I was worried about the size of the friends table, I mean.. isn't it going to explode after a while? Mar 15, 2017 at 12:12
  • @PauloQueiroz Makes no difference. This is going to be a small easy and fast table. Up to 50-80 million rows on a good hardware there will be no difference in speed. Way before that you will start getting issues with other queries and would have to start using cache(Memcached, Redis). As well as you can look in to sharding if your version of MySQL is 5.1+
    – E_p
    Mar 15, 2017 at 14:47
  • I see.. Would you recommend me changing ON DELETE NO ACTION to ON DELETE CASCADE? Mar 15, 2017 at 15:23
  • @PauloQueiroz Deleting is a very expensive operation for big tables and it can corrupt your data. It is better to just have a bool field deleted (in user table) and just mark records. On smaller applications ON DELETE CASCADE is a totally valid option.
    – E_p
    Mar 15, 2017 at 15:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.