2016-06-13 53 views
1

我堅持了一個問題,我請大家幫忙查詢與工會,加入和限制(沒有工作)

sql = mysqli_query($con, " 


SELECT piadi, debtorfullName, ClientAddress, mobilePhone, principalAmount, totalAmount, status, product_name,comment ,postdate 
FROM portfeli_0 
INNER JOIN komentarebi 
ON portfeli_0.piadi=komentarebi.person_id 
WHERE portfeli_0.user='$user' 

UNION ALL 
SELECT piadi, debtorfullName, ClientAddress, mobilePhone, principalAmount, totalAmount, status, product_name,comment ,postdate 
FROM portfeli_1 
INNER JOIN komentarebi 
ON portfeli_1.piadi=komentarebi.person_id 
WHERE portfeli_1.user='$user' 

"); 
在komentarebi

我有同樣爲person_id多個記錄,我必須把其中只有1是否有可能?

+0

這看起來並不像SQL服務器? MySQL不等於sql-server。 – Chuck

+0

對於每1行_limit加入(限制不起作用)_,至少對於我來說並不十分清楚。我最初的想法是嘗試將'SELECT'改爲'SELECT DISTINCT's;但不知道更多關於您的數據... – Uueerdo

+0

您正在獲取重複的行?然後使用'UNION'而不是'UNION ALL',這樣重複的東西就被刪除了。如果你要求別的東西向我們展示例子:來自兩個表格的一些行,你得到的結果和你想要的結果。 –

回答

0

這樣的限制應該工作

(SELECT piadi, debtorfullName, ClientAddress, mobilePhone, principalAmount, totalAmount, status, product_name,comment ,postdate 
FROM portfeli_0 
INNER JOIN komentarebi 
ON portfeli_0.piadi=komentarebi.person_id 
WHERE portfeli_0.user='$user' 
LIMIT 1) 

UNION ALL 
(SELECT piadi, debtorfullName, ClientAddress, mobilePhone, principalAmount, totalAmount, status, product_name,comment ,postdate 
FROM portfeli_1 
INNER JOIN komentarebi 
ON portfeli_1.piadi=komentarebi.person_id 
WHERE portfeli_1.user='$user' 
LIMIT 1) 
0

試試這個

(SELECT piadi, debtorfullName, ClientAddress, mobilePhone, principalAmount, totalAmount, status, product_name,comment ,postdate 
FROM portfeli_0 
INNER JOIN komentarebi 
ON portfeli_0.piadi=komentarebi.person_id 
WHERE portfeli_0.user='$user' 
LIMIT 1) 

UNION 

(SELECT piadi, debtorfullName, ClientAddress, mobilePhone, principalAmount, totalAmount, status, product_name,comment ,postdate 
FROM portfeli_1 
INNER JOIN komentarebi 
ON portfeli_1.piadi=komentarebi.person_id 
WHERE portfeli_1.user='$user' 
LIMIT 1) 
+0

在komentarebi我有相同的person_id多個記錄,我必須只採取其中一個是否有可能? –

0

如果您有重複行,請使用UNION而不是UNION ALL將其刪除:

SELECT piadi, debtorfullName, ClientAddress, mobilePhone, principalAmount, totalAmount, status, product_name,comment ,postdate 
FROM portfeli_0 INNER JOIN 
    komentarebi 
    ON portfeli_0.piadi=komentarebi.person_id 
WHERE portfeli_0.user = '$user' 

UNION 

SELECT piadi, debtorfullName, ClientAddress, mobilePhone, principalAmount, totalAmount, status, product_name,comment ,postdate 
FROM portfeli_1 INNER JOIN 
    komentarebi 
    ON portfeli_1.piadi=komentarebi.person_id 
WHERE portfeli_1.user = '$user' ; 

你可以如果您只希望返回一行,請在查詢結尾添加限制。

0

如果在兩個表中都有同一用戶的記錄,那麼postdate可能會不同並且使用UNION而不是UNION ALL不會解決問題(因爲any difference in any of the columns意味着行根據嚴格定義不重複這個詞的,所以unionselect distinct不會刪除它們)

所以不是我建議你選擇與最新的日期行:

SELECT piadi, debtorfullName, ClientAddress, mobilePhone, principalAmount, totalAmount, status, product_name,comment ,postdate 
FROM (
    SELECT piadi, debtorfullName, ClientAddress, mobilePhone, principalAmount, totalAmount, status, product_name,comment ,postdate 
    FROM portfeli_0 
    INNER JOIN komentarebi 
    ON portfeli_0.piadi=komentarebi.person_id 
    WHERE portfeli_0.user='$user' 

    UNION ALL 
    SELECT piadi, debtorfullName, ClientAddress, mobilePhone, principalAmount, totalAmount, status, product_name,comment ,postdate 
    FROM portfeli_1 
    INNER JOIN komentarebi 
    ON portfeli_1.piadi=komentarebi.person_id 
    WHERE portfeli_1.user='$user' 
    ) 
ORDER BY postdate DESC 
LIMIT 1