0

When I use this SQL, the result of mem_name (from the first table) is repeated a lot how can I prevent that?

$sql = "
    SELECT
        mc.mc_id,
        mc.mc_role,
        mc.mc_order,
        mc.mc_order IS NULL AS isnull,
        mem.*
    FROM
        $this->memberCatsTableName mc,
        $this->tableName mem 
    WHERE
        mc.cat_id=$cat_id
        $where
        AND mc.member_id=mem.mem_id
        or mem.mem_name='$mem_name'
    ORDER BY
        isnull ASC,
        mc.mc_order ASC
";      
$query = $this->db->query($sql);
return $query->result_array();
1
  • We do not have any details about your schema. We do not know what $where holds. Using comma-JOINs is not recommended because they can be over looked as JOINs. Injecting variables directly into SQL strings may be destabilizing or dangerous to your app. Your mix of AND and OR logic is likely to be problematic. If a GROUP BY clause is desired, we need to understand how to properly handle the aggregated values in other columns. Commented Nov 3 at 20:06

2 Answers 2

1

Here is a modified query:

$sql = "SELECT mc.mc_id,mc.mc_role,mc.mc_order,mc.mc_order IS NULL AS isnull, mem.*

        FROM $this->memberCatsTableName mc, $this->tableName mem

        WHERE mc.cat_id=$cat_id $where  AND mc.member_id=mem.mem_id or mem.mem_name='$mem_name' 
         
        Group by mem.mem_name 

        ORDER BY isnull ASC, mc.mc_order ASC";

$query = $this->db->query($sql);
return $query->result_array();

You can group by the query. Or you can also specify the join criteria.

Sign up to request clarification or add additional context in comments.

Comments

0

You have to specify a join criteria.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.