简体   繁体   English

加入多个表并从第三个表中获取总行数

[英]Join multiple tables with getting total number of rows from 3rd table

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试图一次加入 4 - 5 张桌子,因为想要获取存储在第 5 张桌子上的 5 张桌子中的多个数据,我正在尝试计算已加入锦标赛的玩家总数,这工作正常,但主要问题是我在这里面临的是,当主表中没有数据时,它仍然返回我 1 行,所有字段为空,除了显示 0 的总玩家,因为它显示不返回任何行,任何人都可以帮助他们下面是我的查询

    $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所以作为回报,total_players 显示为 0,其余全部为空,因为表中没有插入任何数据,但显示它不应该从数据库返回任何数据

You're mixing an aggregate function ( count() ) with plain column names in your SELECT clause and that is giving unexpected results.您在SELECT子句中将聚合函数 ( count() ) 与普通列名混合在一起,这会产生意想不到的结果。 See: Why can't you mix Aggregate values and Non-Aggregate values in a single SELECT?请参阅: 为什么不能在单个 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.您可以通过添加一个包含SELECT子句中所有列名的GROUP BY子句来解决此问题,但具有count()的列名除外。 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:请务必在GROUP BY中完整输入比赛的所有列名tournament.* ,因此请改用tournament.id, tournament.category_id, tournament.game_id等:

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:在 CodeIgniter (3) 中,这将转化为:

$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:或者,您可以使用子选择,在这种情况下,您可以删除与tournament_players表的连接:

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.在 CodeIgniter 中与$this->db->query()一起使用。

I haven't tested these queries obviously, so there may be errors.我显然没有测试过这些查询,所以可能会有错误。 Hopefully this'll help you get started.希望这可以帮助您入门。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM