独特的语句用于一次只返回一个唯一结果mysql

时间:2019-06-18 05:26:30

标签: php mysql

有三个表:

tbl住宿表

--------------  -----------------------
fldProductID     fldAccommodationName
--------------  -----------------------

tbl住宿表

-------------  --------------------  ---------------------
fldProductID   fldAccommodationText  fldAccommodationText1
-------------  --------------------  ---------------------

tbl住宿表

------------  --------
fldProductID  seo_url
------------  --------

三个具有不同

的表联接查询
$sql = "SELECT 
        DISTINCT 
        tblAccommodation.fldProductID,
        tblAccommodationText.fldAccommodationTextTitle,
        seo_url,
        tblAccommodation.fldAccommodationName
    FROM tblAccommodation 
    LEFT JOIN tblAccommodationText 
        ON tblAccommodationText.fldProductID = tblAccommodation.fldProductId
    LEFT JOIN tblProduct
        ON tblProduct.fldProductID = tblAccommodation.fldProductID
    WHERE
        seo_url = 'yacht-4'";

代码结果

------------  -------------------------  -------  --------------------
fldProductID  fldAccommodationTextTitle  seo_url  fldAccommodationName
------------  -------------------------  -------  --------------------
6                 Dolphin Villas         yacht-4     yacht 4 upper cabin
6                 Aqua Suite             yacht-4     Aqua Suite with Pool
6                 Dolphin Villas         yacht-4     yacht 4 upper cabin
6                 Aqua Suite             yacht-4     Aqua Suite with Pool

为什么重复两次相同的值,请帮助我

3 个答案:

答案 0 :(得分:0)

您可以使用GROUP BY

SELECT tblAccommodation.fldProductID,fldAccommodationText1,tblAccommodationText.fldAccommodationTextTitle,seo_url,tblAccommodation.fldAccommodationName 
    FROM tblAccommodation 
        LEFT JOIN tblAccommodationText ON tblAccommodationText.fldProductID = tblAccommodation.fldProductID 
        LEFT JOIN tblProduct ON tblProduct.fldProductID = tblAccommodation.fldProductID 
        WHERE seo_url='yacht-4' 
    GROUP by tblAccommodationText.fldAccommodationTextTitle

答案 1 :(得分:0)

代码首先工作,我删除了不同的代码,然后按功能分组添加..

correct query is

$sql = "SELECT tblAccommodation.fldProductID,fldAccommodationText1,tblAccommodationText.fldAccommodationTextTitle,seo_url,tblAccommodation.fldAccommodationName FROM tblAccommodation LEFT JOIN tblAccommodationText ON tblAccommodationText.fldProductID = tblAccommodation.fldProductID LEFT JOIN tblProduct ON tblProduct.fldProductID = tblAccommodation.fldProductID where seo_url='yacht-4' GROUP by tblAccommodationText.fldAccommodationTextTitle"

答案 2 :(得分:-1)

tbl住宿表

--------------  -----------------------
fldProductID     fldAccommodationName
--------------  -----------------------
 6                yacht 4 upper cabin
 6                Aqua Suite with Pool

tblAccommodationText表

--------------  --------------------- ------------------------ 
fldProductID     fldAccommodationText  fldAccommodationText1
--------------  ---------------------  ------------------------
 6                Dolphin Villas        king sized bed overlooking the ocean...
 6                Aqua Suite            sundeck with lagoon access where you...

tblProduct表

--------------  --------
fldProductID     seo_url 
--------------  --------
 6               yacht-4 
 6               yacht-4

预期表

------------           ----------------------------    --------------------  --------
fldProductID          fldAccommodationText1            fldAccommodationText  seo_url 
------------          -----------------------------    --------------------  --------
6             king sized bed overlooking the ocean...     Dolphin Villas      yacht-4
6             sundeck with lagoon access where you...    Aqua Suite           yacht-4