[英]Remove Null Values from Left join MYSQL & PHP
我有這個數據庫,我在上面執行了Left Join,我有Null值,我想刪除這些Null值。 我正在附加顯示查詢的數據庫和PHP腳本。
如果有人可以幫我從JSON中刪除Null值嗎? 我想根據感興趣的團隊表檢索用戶感興趣的所有新聞。
團隊興趣表
teaminterests_id |userName |teamName
-------------------------------------------------
1 | Tomas | Real Madrid
--------------------------------------------------
2 | Tomas | FCB. Barcelona
--------------------------------------------------
3 | Carl | Real Madrid
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
如果我將此URL發送到此PHP SCRIPT
http://localhost/HiFootball/NewsFragment/test.php?userName = tomas
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);
?>
我收到此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}
]
從移動部分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.