在 MySQL 中将多个日期插入一行的最佳方法

发布于 2024-12-04 02:59:11 字数 302 浏览 0 评论 0原文

我有一个用于预订多个日期广告的表单,这些日期是通过使用表单中的日历来选择的,然后以 JSON 格式发布。一个例子是:

[20110915, [20110918, 20110925], 20110926, 20110927]

但我在为每个广告存储这个 json 字符串时遇到问题。

  1. 您建议如何存储与广告相关的日期?
  2. 如何使用其中一个日期选择广告。

我不想将每个日期单独存储在不同的表(这使得数据库变得庞大)。

I have a form to book ads for multiple dates, which are selected by using a calendar in the form and then posted in JSON format. An example would be:

[20110915, [20110918, 20110925], 20110926, 20110927]

but I am having a problem storing this json string for each ad.

  1. How would you recommend storing this dates with a relation to the ad?
  2. How to select an ad using one of its dates.

I don't want to store each date separately in different table (which makes db bulky).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑾兮 2024-12-11 02:59:11

您建议如何存储与广告相关的日期?

您应该将它们存储在关系表中,其中包含广告 ID 和预订日期。

ad_bookings

ad_id | booking_date

PRIMARY KEY 将为复合(ad_id, booking_date)

样本数据为:

ad_id        | booking_date
---------------------------
1            | 2011-09-15
1            | 2011-09-18
1            | 2011-09-19
1            | 2011-09-20
1            | 2011-09-26
2            | 2011-09-27
2            | 2011-09-30

如何使用其中一个日期选择广告。

    SELECT *
      FROM ads a
INNER JOIN ad_bookings ab
        ON a.ad_id = ab.ad_id
       AND ab.booking_date = '2011-09-30'

How would you recommend storing this dates with a relation to the ad?

You should store them in a relationship table, which will have the id of the ad and the date of the booking.

ad_bookings

ad_id | booking_date

The PRIMARY KEY would be the compound (ad_id, booking_date).

Sample data would be:

ad_id        | booking_date
---------------------------
1            | 2011-09-15
1            | 2011-09-18
1            | 2011-09-19
1            | 2011-09-20
1            | 2011-09-26
2            | 2011-09-27
2            | 2011-09-30

How to select an ad using one of its dates.

    SELECT *
      FROM ads a
INNER JOIN ad_bookings ab
        ON a.ad_id = ab.ad_id
       AND ab.booking_date = '2011-09-30'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文