数据库布局问题
我会尽力解释这个问题。
我有这样的结构:
offers
--------------
id_offer|offer|company
1 | web programmer| Microsoft
2 | web programmer| Microsoft
tags
--------------
id_tags | tags
1 | php
2 | js
3 | asp
offers_has_tags
---------------
offers_id_offer (fk) | tags_id_tags (fk)
1 | 1
1 | 2
1 | 3
2 | 1
2 | 2
如果我使用像 Stackoverflow 这样的系统,其中每个问题的标题都是唯一的,那么下面的代码就没有问题。但是,如果我可以拥有具有相同标题和相同所有者的各种职位,我就无法执行 WHERE Offers = ?
,
因此,我需要采用不同的方法来选择特定工作。不能是标题,也不能是所有者+标题,因为同一所有者可以拥有具有相同标题的多个优惠。
INSERT INTO `offers_has_tags` (
offers_id_offer,
tags_id_tags
)
VALUES (
(SELECT id_offer FROM offers WHERE offer = ?), //here
(SELECT id_tags FROM tags WHERE tags = ?))
当存在多个具有相同标题和相同所有者的报价时,我该如何选择?
i will try to explain the problem.
I have this structure:
offers
--------------
id_offer|offer|company
1 | web programmer| Microsoft
2 | web programmer| Microsoft
tags
--------------
id_tags | tags
1 | php
2 | js
3 | asp
offers_has_tags
---------------
offers_id_offer (fk) | tags_id_tags (fk)
1 | 1
1 | 2
1 | 3
2 | 1
2 | 2
If i use a system like Stackoverflow
, where each title of question is unique, there is no problem with the code below. But if i can have various offers with same title, and with same owner, i can't do WHERE offers = ?
,
So, i need a different approach to select a specific job. Can't be the title, and can't be the owner+title, because the same owner can have various offers with same title.
INSERT INTO `offers_has_tags` (
offers_id_offer,
tags_id_tags
)
VALUES (
(SELECT id_offer FROM offers WHERE offer = ?), //here
(SELECT id_tags FROM tags WHERE tags = ?))
How can i select an offer when exists more than one, with same title and same owner ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简单的答案:如果您的 where 子句未按 PK 列过滤行,则无法从表中准确检索一行。
您想要实现的目标并不是 100% 清楚。但是,主键用于唯一标识行。这意味着在这种情况下您应该在where子句中使用offer_id(在原始问题的插入语句中)。
我猜您在此之前有一些 UI - 为什么您的 UI 不将 Offer_id 发送到数据访问代码而不是报价名称?
但是,如果您想将所有具有相同名称和所有者的报价插入到 Offer_has_tag 中,请尝试以下操作(这是 T-SQL 语法,但据我记得它也应该适用于 MySQL):
请注意,您应该使用 id_tags 而不是查询中的标签名称。仅使用描述性属性来过滤最终用户的列表。否则,请使用主键列。
Simple answer: there is no way to retrieve exactly one row from table if your where clause is not filtering rows by PK columns.
It is not 100% clear what You are trying to achieve. However, primary key is used to uniquely identify the row. This means that in this case You should use offer_id in where clause (in your insert statement in original question).
I guess that you have some UI in front of this - why don't your UI send offer_id to data access code instead of offer name?
But, if You want to insert all offers with same name and owner to offer_has_tag, try this (it is T-SQL syntax but as far as I can recall it should work on MySQL also):
Please note that You should use id_tags instead of tag name in your queries. Only use descriptive attributes in filtering the list for end user. Otherwise, use primary key columns.
你能说得更具体一点吗?您在什么情况下执行 sql,出于什么目的以及以何种方式给出参数?也许发布这些语句前面的代码。
这可以帮助回答你的问题。我现在想知道的是,为什么得到多个案例结果会出现问题?
Could you be a bit more specific? In which situation are you executing the sql, for which purpose and in which way are the arguments given? Maybe post the code that's in front of these statements.
That could helping answer your question. The thing I'm wondering at the moment, why is it a problem to get more than one case as result?