简体   繁体   English

MYSQL内部联接三到四个表

[英]MYSQL Inner join for three to four tables

Please I need to figure out what I am doing wrong. 请我需要弄清楚我在做什么错。 I created this inner join code for mysql. 我为mysql创建了此内部连接代码。 it works but it gives me repeated values like repeating a particular row twice or categoryid twice. 它有效,但是却给了我重复的值,例如重复两次特定行或两次categoryid。 each of the tables(users,paymentnotification,monthlyreturns) has the categoryid used to check and display the username(users.pname) from the user table, then check and display those that have made payment from the monthly returns and payment table using the categoryid. 每个表(“用户”,“付款通知”,“月度回报”)都具有categoryid,用于检查和显示用户表中的用户名(users.pname),然后检查并显示使用categoryid从月度回报和付款表中付款的用户。

$r="SELECT monthlyreturns.categoryid, monthlyreturns.month, monthlyreturns.quarter, monthlyreturns.year,paymentnotification.amount, users.pname, monthlyreturns.ototal, paymentnotification.payee, status
FROM paymentnotification 
INNER JOIN (monthlyreturns INNER JOIN users ON monthlyreturns.categoryid=users.categoryid) 
ON monthlyreturns.categoryid=paymentnotification.categoryid 
ORDER BY monthlyreturns.categoryid DESC";

I think the query you want is more like this: 我认为您想要的查询更像这样:

SELECT b.categoryid, b.month, b.quarter, b.year, a.amount, c.pname, b.ototal, a.payee, status
FROM paymentnotification a
INNER JOIN monthlyreturns b
   ON a.categoryid = b.categoryid
INNER JOIN users c
   ON b.categoryid = c.categoryid
ORDER BY b.categoryid DESC

The way you are doing the correlations doesn't seem clear and may cause problems. 您进行关联的方式似乎不清楚,可能会引起问题。 Try this one out and see what happens. 试试这个,看看会发生什么。 If its still doing duplicates, perhaps the nature of the data require further filtering. 如果它仍在重复,则数据的性质可能需要进一步过滤。

Assuming I understand what you're trying to do, you are not joining your tables properly. 假设我了解您要执行的操作,则说明您未正确加入表。 Try joining one at a time 尝试一次加入一个

SELECT DISTINCT monthlyreturns.categoryid, monthlyreturns.month, monthlyreturns.quarter, monthlyreturns.year,paym entnotification.amount, users.pname, monthlyreturns.ototal, paymentnotification.payee, status
FROM paymentnotification 
INNER JOIN monthlyreturns
ON paymentnotification.categoryid = monthlyreturns.categoryid
INNER JOIN users 
ON monthlyreturns.categoryid = users.categoryid
ORDER BY monthlyreturns.categoryid DESC

I don't see any problem.. I get 4 result rows: check this fiddle http://sqlfiddle.com/#!2/165a22/5 我没看到任何问题。.我得到4条结果行:检查此小提琴http://sqlfiddle.com/#!2/165a22/5

this is the query I used: 这是我使用的查询:

SELECT m.categoryid, m.month, m.quarter, m.year,p.amount, u.pname, m.ototal, p.payee, m.status
FROM paymentnotification p JOIN monthlyreturns m ON p.categoryid = m.categoryid
JOIN users u ON u.categoryid = m.categoryid
ORDER BY m.categoryid DESC

there are no duplicated rows, just "unique" rows if you consider every column you choose. 如果您考虑选择的每一列,则没有重复的行,只有“唯一”行。

Hope it helps 希望能帮助到你

SELECT M.categoryid, M.month, M.quarter, M.year, M.ototal, SELECT M.categoryid,M.month,M.quarter,M.year,M.ototal,

   P.amount, P.payee, P.status,

   U.pname

FROM paymentnotification AS P 从付款通知AS P

INNER JOIN monthlyreturns AS M ON P.categoryid = M.categoryid INNER JOIN月收益P.categoryid = M.categoryid

INNER JOIN users AS U ON M.categoryid = U.categoryid INNER JOIN用户AS U ON M.categoryid = U.categoryid

ORDER BY M.categoryid DESC 按M.categoryid DESC排序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM