Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 连接表时获得重复结果_Php_Mysql - Fatal编程技术网

Php 连接表时获得重复结果

Php 连接表时获得重复结果,php,mysql,Php,Mysql,当我使用Select语句显示Mysql数据库中的数据时。我得到了重复的值。如何解决这个问题?我的sql查询如下: $sql="SELECT r.hosteladmissionno,r.student_name,r.semester, m.billmonth,m.billyear,m.wastagecharge,b.exp_amount FROM registration r, messexp m, blockexp b WHERE r.mess_type = '".$

当我使用Select语句显示Mysql数据库中的数据时。我得到了重复的值。如何解决这个问题?我的sql查询如下:

$sql="SELECT r.hosteladmissionno,r.student_name,r.semester,
         m.billmonth,m.billyear,m.wastagecharge,b.exp_amount
  FROM  registration r, messexp m, blockexp b
  WHERE r.mess_type = '".$q."' AND r.mess_type=m.messtype AND r.status_flag=1";

您应该在查询中使用内部联接。否则,每个注册结果将与每个注册结果合并

$sql="SELECT registration.hosteladmissionno,
    registration.student_name,
    registration.semester,
    messexp.billmonth,
    messexp.billyear,
    messexp.wastagecharge,
    blockexp.exp_amount
FROM registration
INNER JOIN messexp ON (messexp.id_registration = registration.id)
INNER JOIN blockexp ON (blockexp.id_messexp = messexp.id)
 WHERE 
registration.mess_type = '".$q."' AND status_flag=1";

请注意,(…)将根据您的架构而改变

您应该在查询中使用内部联接。否则,每个注册结果将与每个注册结果合并

$sql="SELECT registration.hosteladmissionno,
    registration.student_name,
    registration.semester,
    messexp.billmonth,
    messexp.billyear,
    messexp.wastagecharge,
    blockexp.exp_amount
FROM registration
INNER JOIN messexp ON (messexp.id_registration = registration.id)
INNER JOIN blockexp ON (blockexp.id_messexp = messexp.id)
 WHERE 
registration.mess_type = '".$q."' AND status_flag=1";

请注意,(…)上的值将根据您的模式而改变,因为您已经交叉连接了表,所以您可以得到结果的每个组合。 连接where子句中的表:

select tab1.column1, table2.column1
from tab1, tab2
where tab1.fkColumn = tab2.idColumn

哪些列将这些表相互连接起来?

您已经交叉连接了这些表,因此可以得到每个结果组合。 连接where子句中的表:

select tab1.column1, table2.column1
from tab1, tab2
where tab1.fkColumn = tab2.idColumn

哪些列将表彼此连接起来?

请格式化SQL,这样我们就不必滚动使用
DISTINCT
AFTER
选择
DISTINCT在所有列上提供一行DISTINCT。您确定在所有选定的列中都有一个值相同的完全重复的行吗?
status\u flag
属于哪个表?我想其中一个编辑已经删除了初始帖子中的DISTINCT关键字。请格式化SQL,这样我们就不必在
select
DISTINCT之后滚动使用
DISTINCT
在所有列上提供不同的行。您确定在所有选定列中都有一个值相同的完全重复的行吗?
status\u flag
属于哪个表?我想其中一个编辑已删除了初始帖子中的DISTINCT关键字。谢谢,我将尝试它..freye。。id_注册从何而来?@Dinzy这是对你的模式的假设。通常,要在两个表之间建立关联,需要使用来自两个表之一的id。例如,表格=>Student(id、lastname、firstname、id_class…)和class(id、name、description等)。学生和班级之间有联系。这意味着该学生在课堂上被id_类引用谢谢我会试试..freye。。id_注册从何而来?@Dinzy这是对你的模式的假设。通常,要在两个表之间建立关联,需要使用来自两个表之一的id。例如,表格=>Student(id、lastname、firstname、id_class…)和class(id、name、description等)。学生和班级之间有联系。这意味着学生在id_类引用的类中。您还必须连接表blockexp。否则您将像以前一样交叉连接它。您还必须连接表blockexp。否则你会像以前一样交叉连接它。