SQL 查询返回假的重复结果?
我一直在尝试编写一些供个人使用的 WHMCS 小插件。本质上,我在这里想做的是获取有关特定订单的一堆信息,并将其作为 Perl 中的数组返回。
我对 Perl 位很满意,这是我形成的 MySQL 查询给我带来了压力。
我知道它又大又乱,但我所拥有的是:
SELECT tblhosting.id, tblhosting.userid, tblhosting.orderid, tblhosting.packageid, tblhosting.server, tblhosting.domain, tblhosting.username, tblorders.invoiceid, tblproducts.gid, tblservers.ipaddress, tblinvoices.status
FROM tblhosting, tblproducts, tblorders, tblinvoices, tblservers
WHERE tblorders.status = 'Pending'
AND tblproducts.gid = '2'
AND tblservers.id = tblhosting.server
AND tblorders.id = tblhosting.orderid
AND tblinvoices.id = tblorders.invoiceid
AND tblinvoices.status = 'Paid'
我不知道这是否/应该/工作,但我假设我走在正确的轨道上,因为它确实返回了我正在寻找的内容,但是它会返回所有内容两次。
例如,我创建了一个域为“sunshineee.info”的新帐户,然后在 PHPMyAdmin 中运行上述查询。
id userid orderid packageid server domain username invoiceid gid ipaddress status
13 7 17 6 1 sunshineee.info sunshine 293 2 184.22.145.196 Paid
13 7 17 6 1 sunshineee.info sunshine 293 2 184.22.145.196 Paid
任何人都可以告诉我我在这个问题上出了什么问题吗?显然(也许还不够明显)我希望每场比赛只返回一行。我已经在 > 1 域中尝试过了数据库,它返回每个匹配项的重复项。
任何帮助将不胜感激 :)
I've been trying to write a few little plugins for personal use with WHMCS. Essentially what I'm trying to do here is grab a bunch of information about a certain order(s), and return it as an array in Perl.
The Perl bit I'm fine with, it's the MySQL query I've formed that's giving me stress..
I know it's big and messy, but what I have is:
SELECT tblhosting.id, tblhosting.userid, tblhosting.orderid, tblhosting.packageid, tblhosting.server, tblhosting.domain, tblhosting.username, tblorders.invoiceid, tblproducts.gid, tblservers.ipaddress, tblinvoices.status
FROM tblhosting, tblproducts, tblorders, tblinvoices, tblservers
WHERE tblorders.status = 'Pending'
AND tblproducts.gid = '2'
AND tblservers.id = tblhosting.server
AND tblorders.id = tblhosting.orderid
AND tblinvoices.id = tblorders.invoiceid
AND tblinvoices.status = 'Paid'
I don't know if this /should/ work, but I assume I'm on the right track as it does return what I'm looking for, however it returns everything twice.
For example, I created a new account with the domain 'sunshineee.info', and then in PHPMyAdmin ran the above query.
id userid orderid packageid server domain username invoiceid gid ipaddress status
13 7 17 6 1 sunshineee.info sunshine 293 2 184.22.145.196 Paid
13 7 17 6 1 sunshineee.info sunshine 293 2 184.22.145.196 Paid
Could anyone give me a heads up on where I've gone wrong with this one.. Obvioiusly (maybe not obviously enough) I want this as only one row returned per match.. I've tried it with >1 domain in the database and it returned duplicates for each of the matches..
Any help would be much appreciated
:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,如果没有任何表定义,这几乎是不可能的,但是您在那里做了很多连接。您从 tblhosting.id 开始,然后从那里开始“向上”工作。如果任何连接的表具有双条目,您将获得更多命中。
您可以向查询添加
DISTINCT
,但这并不能解决根本问题。这可能是您的数据有问题:您有 2 张发票吗?也许您应该选择所有内容(SELECT * FROM
)并检查返回的内容,也许检查您的表格是否有双重内容。大多数情况下,使用 DISTINCT 并不是一个好的选择:这意味着您的查询或数据不正确(或者您没有完全理解它们)。它可能暂时给你带来正确的结果,但以后可能会给你带来麻烦。
对发生这种情况的原因的猜测:
您没有将产品表连接到 ID 链。因此,据我所知,您基本上是在结果中添加了“2”。您加入产品,唯一限制该表的是“gid”应该为 2。因此,如果您添加 gid 2 的产品,您会得到另一个结果。要么加入它(也许 tblproduct.orderid = tblorders.id?只是在这里猜测),要么只是删除它,因为据我所知它没有任何作用。
如果您想让查询更清晰一点,请尝试不要隐式联接,而是这样做。这样你就可以真正看到发生了什么
Well, its near impossible without any table definitions, but you are doing a lot of joins there. You are starting with tblhosting.id and working your way 'up' from there. If any of the connected tables has a double entry, you'll get more hits
You could add a
DISTINCT
to your query, but that would not fix the underlying issue. It could be a problem with your data: do you have 2 invoices? Maybe you should select everything (SELECT * FROM
) and check what is returned, maybe check your tables for double content.Using
DISTINCT
is most of the time not a good choice: it means either your query or your data is incorrect (or you don't understand them thoroughly). It might get you the right result for now, but can get you in trouble later.A guess about the reason this happens:
You do not connect the products table to the chain of id's. So you are basically adding a '2' to your result as far as I can see. You join on products, and the only thing that limits that table is that "gid" should be 2. So if you add a product with gid 2 you get another result. Either join it (maybe tblproduct.orderid = tblorders.id ? just guessing here) or just remove it, as it does nothing as far as I can see.
If you want to make your query a bit clearer, try not implicitly joining, but do it like this. So you can actually see what's happening
我在您的查询中没有看到 JOIN to
tblproducts
,这似乎是一个原因。I don't see in your query JOIN to
tblproducts
, it seems to be a reason.