在 mysql 中存储和检索用户收藏夹

Store and retrieve user favorites in mysql

我有一个 android 应用程序可以从 mysql db 中提取信息并将其显示在列表中。每个列表项都有一个 favorite 按钮,单击该按钮会使用用户名和列表项更新数据库。

我可以使用关系键和外键获取所有用户收藏夹。 我想要实现的是,当用户 re-logs 进入应用程序并滚动查找可用项目时,他已经可以看到他标记为最喜欢的项目,而无需转到他的收藏夹页面。

例如,当您滚动 Facebook 页面时,您已经可以看到您喜欢的帖子。

我怎样才能做到这一点?

第一个table

|------------------|--------------|----|-------|
|      Column      |     Type     |Null|Default
|------------------|--------------|----|-------|
|    //**id**//    |   int(11)    | No |
|    car_vendor    |  varchar(20) | No |
|    car_model     |  varchar(20) | No |
|    car_petro     |  varchar(10) | No |
| car_transmition  |  varchar(10) | No |
|     car_hand     |   int(11)    | No |
|  car_spedometer  |  varchar(7)  | No | 
|  car_engine_vol  |  varchar(4)  | No |
|   car_category   |   int(11)    | No |
|  car_post_date   |     date     | No |

秒table

|----------------|------------|----|
|     Column     |    Type    |Null| Default
|----------------|------------|----|
|   //**id**//   |   int(11)  | No |
|    username    | varchar(20)| No |
|  favorites_id  |   int(11)  | No | <<< foreign key points to ID on first table 

You can ask the db for that information,

the moment you get the list of all products you can do a LEFT JOIN, that can be used to connect a product, if liked, to the user. You can do something like this.

SELECT CAR_VENDOR,
    A.CAR_MODEL,
    A.CAR_PETRO,
    A.CAR_TRANSMITION,
    A.CAR_HAND,
    A.CAR_SPEDOMETER,
    A.CAR_ENGINE_VOL,
    A.CAR_CATEGORY,
    A.CAR_POST_DATE,
    B.USERNAME
FROM TABLE_ONE AS A
LEFT JOIN TABLE_TWO AS B ON B.FAVORITES_ID = A.ID
AND B.USERNAME = "actual user username"

This query should do the work, I didn't test it but you can take as an idea where to start

Only the object with the preference should have the username field filled

EDIT ------------------------------------------------

Taking the case that the OP presented we have that:

  • We have a single table that is a list of objects (cars) with their specific parameters (engine size, hand, model, category etc)
  • We have a N to N table that can connect each product to one or more user and each user to one or more product
  • We probably have a table of people (the "username" column of table two, that I wish to call "LIKES" should be a foreign key connected to the primary key in PEOPLE table)

The behavior is simple, we want something social-like where a person can "upvote" a car, obviously we don't want multiple votes on the same car from the same person thus we have to disable the function in case he/she has already voted.

We take in example a normal page in which a person has already voted some products and want to see the complete list.

In this case we want to see:

  • The COMPLETE list of item, no matter what.
  • The items that have already got a vote from that particular user.

One way to handle this is connecting CARS table with LIKES table (I leave the anagraphic of user outside to simplify).

The way to do this is using a JOIN connecting the two tables on the id of the car.

This SQL command will return a set of line that are the combination of each line inside the CARS followed by the relative username.

This will lead to mutiple lines for the same CAR, one for each upvoting user, needing a business logic to handle the problem of showing the correct page.

A step further in SQL logic is to use a LEFT JOIN.

The LEFT JOIN will include ALL the record from the first table and will connect them with the matching line of the second one.

Thus joining CARS with LIKE on car-id AND asking for the username that we want, will return the complete list of cars (all record from left) connected to (and only to) the records that match the username related condition.

The example can be this:

|         CARS         |
|  ID  |     MODEL     |
|------+---------------|
|   1  +     FORD      |
|   2  +     OPEL      |
|   3  +    FERRARI    |
|   4  +      GM       |
|----------------------|

|        LIKES         |
|  USERNAME  |  CAR_ID |
|------------+---------|
|   KEVIN    |    3    |
|    PAUL    |    3    |
|------------+---------|

If I use a simple JOIN like this:

SELECT ID, MODEL, USERNAME
FROM CARS
JOIN LIKES ON CAR_ID = ID

It will return this:

|  ID  |   MODEL   |  USERNAME  |
|   3  |  FERRARI  |   KEVIN    |
|   3  |  FERRARI  |    PAUL    |

That is incomplete

If I use something like this:

SELECT ID, MODEL, USERNAME
FROM CARS
LEFT JOIN LIKES ON CAR_ID = ID

I will have this:

|  ID  |   MODEL   |  USERNAME  |
|   1  |   FORD    |    NULL    |
|   2  |   OPEL    |    NULL    |
|   3  |  FERRARI  |   KEVIN    |
|   3  |  FERRARI  |    PAUL    |
|   4  |    GM     |    NULL    |

That is still incorrect, even if better.

The double record for the FERRARI will be removed adding a condition to the join:

SELECT ID, MODEL, USERNAME
FROM CARS
LEFT JOIN LIKES ON CAR_ID = ID AND USERNAME = "KEVIN"

This will return:

|  ID  |   MODEL   |  USERNAME  |
|   1  |   FORD    |    NULL    |
|   2  |   OPEL    |    NULL    |
|   3  |  FERRARI  |   KEVIN    |
|   4  |    GM     |    NULL    |

That is the correct list.

Once I've got this list I need to show it to the user, checking

if USERNAME is not NULL then DISABLE LIKE BUTTON

(PSEUDOCODE)

I hope this is useful to somebody