关系表:具有关系的第三个表

发布于 2024-10-07 06:31:09 字数 724 浏览 10 评论 0原文

我正在构建一个 php 页面,当用户在表单字段中输入他/她的电话号码时,该页面将显示特定的横幅。

所以这是我的数据库逻辑:

表电话与字段:id、用户名和电话号码。 表横幅,其中包含以下字段:id、横幅的名称/标题和横幅(图像的路径)。 表与字段的关系:这里是电话号码应该与横幅相关的地方,也是我需要你帮助的地方:)

这是我的 php 页面的逻辑:

-form 获取电话号码 -我查询数据库 -我显示与表单中输入的电话号码相关的横幅。

下面是到目前为止创建表的代码..您将看到不知道如何前进。

感谢一百万

    CREATE TABLE phones(

id_phone INT NOT NULL AUTO_INCREMENT,

nombre VARCHAR(30),

number INT (9),

PRIMARY KEY (id_phone)

) TYPE = INNODB;


CREATE TABLE banners (

id_banners INT NOT NULL AUTO_INCREMENT,

id_phone INT NOT NULL,

name VARCHAR(250),

banner VARCHAR(250),

PRIMARY KEY(id_phone),

INDEX (id_phone),

FOREIGN KEY (id_phone) REFERENCES clientes(id_phone)

) TYPE = INNODB;

I'm building a php page that will show an specific banner when the user enters his/her phone number in a form field.

So here's my logic for the database:

Table phones with fields: id, name of user and phone number.
Table banners with fields: id, banner's name/title and banner (path to the image).
Table relation with fields: here's where the phone number should be related to a banner and where I need your help :)

And here's my logic for the php page:

-form gets the phone number
-I query the data base
-I show the banner related to the phone number entered in the form.

Below is the code for the table creation so far .. as you'll see don't know how to advance.

Thanks a million

    CREATE TABLE phones(

id_phone INT NOT NULL AUTO_INCREMENT,

nombre VARCHAR(30),

number INT (9),

PRIMARY KEY (id_phone)

) TYPE = INNODB;


CREATE TABLE banners (

id_banners INT NOT NULL AUTO_INCREMENT,

id_phone INT NOT NULL,

name VARCHAR(250),

banner VARCHAR(250),

PRIMARY KEY(id_phone),

INDEX (id_phone),

FOREIGN KEY (id_phone) REFERENCES clientes(id_phone)

) TYPE = INNODB;

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

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

发布评论

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

评论(2

旧竹 2024-10-14 06:31:09

从您的问题来看,似乎每个电话号码都只有一个与之关联的横幅。因此,删除 banners.id_banners 字段并添加 phones.id_banners

然后选择数据,可以进行 JOIN:

SELECT phones.id_phone, phones.nombre, phones.number, 
banners.id_banners, banners.name, banners.banner
JOIN banners ON phones.id_banners = banners.id_banners
WHERE phone = '123-456-789'

From your question, it seems that each phone number has only one banner associated with it. Therefore, delete the banners.id_banners field and add a phones.id_banners

Then to select the data, you can do a JOIN:

SELECT phones.id_phone, phones.nombre, phones.number, 
banners.id_banners, banners.name, banners.banner
JOIN banners ON phones.id_banners = banners.id_banners
WHERE phone = '123-456-789'
二货你真萌 2024-10-14 06:31:09

首先,问题是什么?
在这一步我要提到的是,这些实体之间的关系是否是一对一的?如果是这样,您最好将所有这些数据放入一张表中。

++

CREATE TABLE phones(
phone_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30),
number INT (9),
PRIMARY KEY (id_phone)
) TYPE = INNODB;

CREATE TABLE banners (
banner_id INT NOT NULL AUTO_INCREMENT,
phone_id INT NOT NULL,
name VARCHAR(250),
banner VARCHAR(250),
PRIMARY KEY(id_phone),
INDEX (id_phone),
FOREIGN KEY (phone_id) REFERENCES clientes(phone_id)
) TYPE = INNODB;

查询将是:

$query = "SELECT * FROM clients LEFT JOIN banners USING(phone_id) WHERE clients.number='.$number.'";

First of all what's the question?
What can I mention at this step is, does the relation between this entities is one to one? If so, you'd better put all this data into one single table.

++

CREATE TABLE phones(
phone_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30),
number INT (9),
PRIMARY KEY (id_phone)
) TYPE = INNODB;

CREATE TABLE banners (
banner_id INT NOT NULL AUTO_INCREMENT,
phone_id INT NOT NULL,
name VARCHAR(250),
banner VARCHAR(250),
PRIMARY KEY(id_phone),
INDEX (id_phone),
FOREIGN KEY (phone_id) REFERENCES clientes(phone_id)
) TYPE = INNODB;

and the query would be:

$query = "SELECT * FROM clients LEFT JOIN banners USING(phone_id) WHERE clients.number='.$number.'";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文