0

I need to group MySQL result, from property table and category table but I can't understand how can I do it ?

I have 6 category column in property table

SELECT p.*, c.*, p.id id
                  FROM property p
                  JOIN clients c
                    ON p.user_id = c.id
                  LEFT JOIN category ct1
                    ON ct1.id = p.cat1
                  LEFT JOIN category ct2
                    ON ct2.id = p.cat2
                  LEFT JOIN category ct3
                    ON ct3.id = p.cat3
                  LEFT JOIN category ct4
                    ON ct4.id = p.cat4
                  LEFT JOIN category ct5
                    ON ct5.id = p.cat5
                  LEFT JOIN category ct6
                    ON ct6.id = p.cat6
                 WHERE public = '1'
                   AND (p.cat1 = '$cat'
                    OR p.cat2 = '$cat'
                    OR p.cat3 = '$cat'
                    OR p.cat4 = '$cat'
                    OR p.cat5 = '$cat'
                    OR p.cat6 = '$cat')
                 GROUP BY ct1.id, ct2.id, ct3.id, ct4.id, ct5.id, ct6.id
                 ORDER BY p.id DESC

enter image description here

I need 5 result where cat ID is "41"

Thank you


EDITED:

I think this method is not for me, because I have multi category column

can anyone help me?

$SQL = "SELECT p.*, c.*, p.id id
          FROM property p
          JOIN clients c
            ON p.user_id = c.id
         WHERE public = '1'
           AND p.cat1 IN ($cat)
            OR p.cat2 IN ($cat)
            OR p.cat3 IN ($cat)
            OR p.cat4 IN ($cat)
            OR p.cat5 IN ($cat)
            OR p.cat6 IN ($cat)
         ORDER BY p.id DESC";

I get only one result, but I need three result, two times ID 99 and one times 93

enter image description here

Thank you

George B.
  • 172
  • 2
  • 2
  • 14
  • 1
    Why do you have 6 columns with this sort of data? That is normally a symptom of bad design. Could you include the relevant parts of the schema for the 3 tables? – Manngo May 04 '17 at 10:26
  • I'm removed it and now I'm trying make json join: http://stackoverflow.com/questions/43776949/how-to-join-mysql-column-and-json @Manngo – George B. May 04 '17 at 10:33
  • Handle issues of data display in application level code. – Strawberry May 04 '17 at 10:50
  • If you genuinely feel that other answers fail to address this problem, see http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query – Strawberry May 04 '17 at 11:11
  • http://sqlfiddle.com/#!9/2ddd56/4 @Strawberry – George B. May 04 '17 at 11:18
  • Thanks for the progress report. You should have a separate table which says which category belongs to which property. See normalization. – Strawberry May 04 '17 at 11:33

0 Answers0