简体   繁体   English

从左联接MYSQL和PHP中删除空值

[英]Remove Null Values from Left join MYSQL & PHP

I have this data base and I execute Left Join on it and i have Null Values and i want to remove these null values. 我有这个数据库,我在上面执行了Left Join,我有Null值,我想删除这些Null值。 I'm Attaching my Database & PHP Script which shows the Query. 我正在附加显示查询的数据库和PHP脚本。

If anyone Can Help me With removing Null Values from JSON please? 如果有人可以帮我从JSON中删除Null值吗? I want retrieve all the news that a user is interested in depending on the interested teams table. 我想根据感兴趣的团队表检索用户感兴趣的所有新闻。

teaminterest Table 团队兴趣表

teaminterests_id |userName  |teamName     
-------------------------------------------------
1                | Tomas         | Real Madrid       
--------------------------------------------------
2                | Tomas         | FCB. Barcelona
--------------------------------------------------
3                | Carl          | Real Madrid 

teamnews Table teamnews表

    teamnews_id |teamnews_title     |teamnews_image|teamnews_text|teamnews_timeStamp |teamnews_newsURL   |TeamName
    ----------------------------------------------------------------------------------------------------------------------
    3           | Barcelona News 1  | XYZ.jpg      | Dummy Text  |2016-04-23 17:51:23| Dummy Website.com | FCB. Barcelona 
    ----------------------------------------------------------------------------------------------------------------------
    4           | RealMadrid News1  | ZYX.jpg      | Dummy Text  |2016-04-23 17:51:23| Dummy Website.com | Real Madrid 
 ----------------------------------------------------------------------------------------------------------------------
    5           | RealMadrid News2  | ZYX.jpg      | Dummy Text  |2016-04-23 17:51:23| Dummy Website.com | Real Madrid  
   ----------------------------------------------------------------------------------------------------------------------
    6           | RealMadrid News3  | ZYX.jpg      | Dummy Text  |2016-04-23 17:51:23| Dummy Website.com | Real Madrid 
 ----------------------------------------------------------------------------------------------------------------------
    7           | Barcelona News 2  | XYZ.jpg      | Dummy Text  |2016-04-23 17:51:23| Dummy Website.com | FCB. Barcelona 

And if i Send This URL to this PHP SCRIPT 如果我将此URL发送到此PHP SCRIPT

http://localhost/HiFootball/NewsFragment/test.php?userName=tomas http://localhost/HiFootball/NewsFragment/test.php?userName = tomas

PHP SCRIPT PHP脚本

<?php

require('C:\wamp\www\HiFootball\config.php');
$conn = mysqli_connect($servername, $username, $password, $db);



    $query="
   select  distinct * 

    from teaminterest

    left join teamnews   

    on  teaminterest.userName='".$_GET['userName']."'  and teamnews.TeamName=teaminterest.teamName 
    order by teamnews_timeStamp desc
    ";


$result = mysqli_query($conn,$query);
$rows = array();
echo mysqli_error($conn);

while($row = mysqli_fetch_assoc($result)) {

    $rows[] = $row;
}

echo json_encode($rows);


?> 

I get this JSON FORM: 我收到此JSON FORM:

   [
{"teaminterests_id":"104","userName":"Tomas","teamName":"Real Madrid","teamnews_id":"4","teamnews_title":"Real Madrid News 1 ","teamnews_image":"ZYX.jpg","teamnews_text":"Dummy Text","teamnews_timeStamp":"2016-04-23 17:52:23","teamnews_newsURL":"www.DummWebsite.com","TeamName":"Real Madrid"}
,
{"teaminterests_id":"104","userName":"Tomas","teamName":"Real Madrid","teamnews_id":"6","teamnews_title":"Real Madrid News 2\r\n","teamnews_image":"ZYX.jpg","teamnews_text":"Dummy Text","teamnews_timeStamp":"2016-04-23 17:52:23","teamnews_newsURL":"www.DummWebsite.com","TeamName":"Real Madrid"}
,
{"teaminterests_id":"104","userName":"Tomas","teamName":"Real Madrid","teamnews_id":"7","teamnews_title":"Real Madrid News 3","teamnews_image":"ZYX.jpg","teamnews_text":"Dummy Text","teamnews_timeStamp":"2016-04-23 17:52:23","teamnews_newsURL":"www.DummWebsite.com","TeamName":"Real Madrid"}
,{"teaminterests_id":"103","userName":"Tomas","teamName":"Barca","teamnews_id":"3","teamnews_title":"Barcelona News 1 ","teamnews_image":"XYZ.jpg","teamnews_text":"Dummy Text","teamnews_timeStamp":"2016-04-23 17:51:23","teamnews_newsURL":"WWW.dummywebsite.com","TeamName":"Barca"}
,{"teaminterests_id":"103","userName":"Tomas","teamName":"Barca","teamnews_id":"8","teamnews_title":"Barcelona News 2","teamnews_image":"XYZ.jpg","teamnews_text":"Dummy Text","teamnews_timeStamp":"2016-04-23 17:51:23","teamnews_newsURL":"WWW.dummywebsite.com","TeamName":"Barca"}
,
{"teaminterests_id":"105","userName":"Carl","teamName":"Real Madrid","teamnews_id":null,"teamnews_title":null,"teamnews_image":null,"teamnews_text":null,"teamnews_timeStamp":null,"teamnews_newsURL":null,"TeamName":null}
]

Move a part from ON tho WHERE and please use proper escaping of your SQL query. 从移动部分ON寿WHERE并请使用您的SQL查询的正确转义。

$query="select  distinct * 
        from teaminterest
        left join teamnews   
            on teamnews.TeamName=teaminterest.teamName 
        where teaminterest.userName='".mysqli_real_escape_string($conn, $_GET['userName'])."' 
        order by teamnews_timeStamp desc
";

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

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