用union,join和limit查询(不工作)

时间:2016-06-13 19:41:51

标签: mysql sql join

我遇到问题请帮忙

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相同,我只能拿其中一个是否可能?

4 个答案:

答案 0 :(得分:0)

这样限制应该起作用

$('#aSearch').keyup(function() {
  var value = $(this).val();
  var exp = new RegExp('\\b' + value, 'i');    
  $('.divA div').each(function() {
    var isMatch = exp.test($('span', this).text());
    $(this).toggle(isMatch);
  });
});

答案 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) 

答案 2 :(得分:0)

如果您有重复的行,请使用var gMapsLoaded = false; window.gMapsCallback = function(){ gMapsLoaded = true; $(window).trigger('gMapsLoaded'); } window.loadGoogleMaps = function(){ if(gMapsLoaded) return window.gMapsCallback(); var script_tag = document.createElement('script'); script_tag.setAttribute("type","text/javascript"); script_tag.setAttribute("src","http://maps.google.com/maps/api/js?sensor=false&callback=gMapsCallback"); (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag); } $(document).ready(function(){ function initialize(){ var mapOptions = { zoom: 18, center: new google.maps.LatLng(51.5207239719, -0.182568696184), mapTypeId: google.maps.MapTypeId.ROADMAP}; map = new google.maps.Map(document.getElementById('map_canvas'),mapOptions); } $(window).bind('gMapsLoaded', initialize); window.loadGoogleMaps(); }); $(window).load(function update () { $.ajax({ url: 'get-last.php', success:function(data){ //Loop through each location. $.each(data, function(){ //Plot the location as a marker var pos = new google.maps.LatLng(this.latitude, this.longitude); new google.maps.Marker({ position: pos, map: map }); }); } , complete: function() { setInterval(update, 1000); } }); 代替UNION删除它们:

UNION ALL

如果只想返回一行,可以在查询结尾添加限制。

答案 3 :(得分: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