0

Trying to join 4 - 5 tables at once as of wanted to grab multiple data which is stored in 5 tables on in 5th table i am trying to count total number of players that have been joined the tournament this is working fine but the main problem which I am facing here is when there is not data in the main table it still return me 1 row with all fields as null except total players showing 0 as it shown not return any rows can anyone help em out below is my query

    $getTournamentData = $this->db->select('tournament.*, tournament_meta.meta_title, tournament_meta.meta_description, categories.title AS category, categories.slug AS category_slug, games.game_name, games.slug AS game_slug, count(tournament_players.id) AS total_players');
    $getTournamentData = $this->db->join('categories', 'categories.id = tournament.category_id');
    $getTournamentData = $this->db->join('games', 'games.game_id = tournament.game_id');
    $getTournamentData = $this->db->join('tournament_meta', 'tournament_meta.post_id = tournament.id');
    $getTournamentData = $this->db->join('tournament_players', 'tournament_players.tournamentID = tournament.id', 'left');


    $dataCond['created_by'] = $this->session->userdata('user_id');

    if($id != null) {
        $dataCond['tournament.id'] = $id;
    }

    $getTournamentData = $this->db->where($dataCond);
    $getTournamentData = $this->db->get('tournament');   

so in return total_players are showing 0 and rest all is null because no data is insterted in the table yet show it should not return any data from the database

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Usman Khan
  • 179
  • 1
  • 17
  • ```COUNT(DISTINCT table3.primary_key_expression)``` – Akina Jun 27 '22 at 09:24
  • @Akina STILL RETURNING ME 1 ROW WITH total_players as 0 – Usman Khan Jun 27 '22 at 09:25
  • Remove the third argument ( `left` ) here: `$this->db->join('tournament_players', 'tournament_players.tournamentID = tournament.id', 'left');` – Marleen Jun 27 '22 at 09:43
  • SELECT `tournament`.*, `tournament_meta`.`meta_title`, `tournament_meta`.`meta_description`, `categories`.`title` AS `category`, `categories`.`slug` AS `category_slug`, `games`.`game_name`, `games`.`slug` AS `game_slug`, count(DISTINCT tournament_players.id) AS total_players FROM `tournament` JOIN `categories` ON `categories`.`id` = `tournament`.`category_id` JOIN `games` ON `games`.`game_id` = `tournament`.`game_id` JOIN `tournament_meta` ON `tournament_meta`.`post_id` = `tournament`.`id` JOIN `tournament_players` ON `tournament_players`.`tournamentID` = `tournament`.`id` – Usman Khan Jun 27 '22 at 10:09
  • @Marleen after removing third the printed query is a follows but it is still returning me 1 row tournament table and rest all tables are empty – Usman Khan Jun 27 '22 at 10:09
  • anyone here to help me out ? – Usman Khan Jun 27 '22 at 10:53
  • use count method –  Jun 28 '22 at 11:25

1 Answers1

1

You're mixing an aggregate function (count()) with plain column names in your SELECT clause and that is giving unexpected results. See: Why can't you mix Aggregate values and Non-Aggregate values in a single SELECT?

You can fix this by adding a GROUP BY clause with all of the column names from the SELECT clause, except for the column name that has the count() on it. Be sure to fully type out all of the column names for the tournament.* in the GROUP BY, so use tournament.id, tournament.category_id, tournament.game_id etc instead:

SELECT tournament.*, tournament_meta.meta_title, tournament_meta.meta_description, categories.title AS category, categories.slug AS category_slug, games.game_name, games.slug AS game_slug, count(tournament_players.id) AS total_players
FROM tournament
JOIN categories ON categories.id = tournament.category_id
JOIN games ON games.game_id = tournament.game_id
JOIN tournament_meta ON tournament_meta.post_id = tournament.id
JOIN tournament_players ON tournament_players.tournamentID = tournament.id
GROUP BY
tournament.id, tournament.category_id, tournament.game_id,
-- add other tournament colums here --
tournament_meta.meta_title, tournament_meta.meta_description, categories.title, categories.slug, games.game_name, games.slug

In CodeIgniter (3) this would translate to:

$this->db->select('tournament.*, tournament_meta.meta_title, tournament_meta.meta_description, categories.title AS category, categories.slug AS category_slug, games.game_name, games.slug AS game_slug, count(tournament_players.id) AS total_players');
$this->db->from('tournament');
$this->db->join('categories', 'categories.id = tournament.category_id');
$this->db->join('games', 'games.game_id = tournament.game_id');
$this->db->join('tournament_meta', 'tournament_meta.post_id = tournament.id');
$this->db->join('tournament_players', 'tournament_players.tournamentID = tournament.id');
$this->db->group_by('tournament.id, tournament.category_id, tournament.game_id,
/* add other tournament columns here */
tournament_meta.meta_title, tournament_meta.meta_description, categories.title, categories.slug, games.game_name, games.slug');

Alternatively you can use a subselect, in which case you can remove the join to the tournament_players table:

SELECT tournament.*, tournament_meta.meta_title, tournament_meta.meta_description, categories.title AS category, categories.slug AS category_slug, games.game_name, games.slug AS game_slug, (
    SELECT count(id)
    FROM tournament_players
    WHERE tournament_players.tournamentID = tournament.id) AS total_players
FROM tournament
JOIN categories ON categories.id = tournament.category_id
JOIN games ON games.game_id = tournament.game_id
JOIN tournament_meta ON tournament_meta.post_id = tournament.id

Use with $this->db->query() in CodeIgniter.

I haven't tested these queries obviously, so there may be errors. Hopefully this'll help you get started.

Marleen
  • 2,245
  • 2
  • 13
  • 23
  • awesome working fine now but if the record exist it's duplicating the rows :( I am using your last provided query – Usman Khan Jun 28 '22 at 10:53
  • Do you have multiple `meta_title`s/`meta_description`s per tournament? That could be causing the duplication. – Marleen Jun 28 '22 at 12:07
  • My bad you are right so what should I do know as i meta title there are 4 rows attached to this tournament i beleive 2 so i need meta data as wel this means i have to create another query to get meta data right – Usman Khan Jun 30 '22 at 11:24
  • Yes, if you want all of the meta data for the tournament, then leave out the join to the meta table and create a separate query to get the meta data for the tournament. – Marleen Jun 30 '22 at 12:07