从数据库中选择Mysql

时间:2016-08-11 08:48:46

标签: php mysql selection

我是新手,我只是在学习。我已经为NHL锦标赛做了一个页面,现在我想缩短我的PHP代码,但实际上我不知道如何。

我的问题是从数据库中选择。它变得非常复杂,但也许有一种方法可以缩短它?

nodetool status

1 个答案:

答案 0 :(得分:1)

您可以在一个查询中合并以下查询。

$center = mysql::rows(mysql::query(1,"SELECT * FROM player_list WHERE team = '$team[team_small]' AND position = 'C'")); 
$defenseman = mysql::rows(mysql::query(1,"SELECT * FROM player_list WHERE team = '$team[team_small]' AND position = 'D'")); 
$goalies = mysql::rows(mysql::query(1,"SELECT * FROM player_list WHERE team = '$team[team_small]' AND position = 'GK'")); 
$leftw = mysql::rows(mysql::query(1,"SELECT * FROM player_list WHERE team = '$team[team_small]' AND position = 'LW'")); 
$rightw = mysql::rows(mysql::query(1,"SELECT * FROM player_list WHERE team = '$team[team_small]' AND position = 'RW'")); 

喜欢这个,

$allplayers=mysql::rows(mysql::query(1,"SELECT * FROM player_list WHERE team = '$team[team_small]' AND position IN ('C', 'D', 'GK', 'LW', 'RW')")); 

现在你可以迭代$allplayers并通过比较position列和简单的PHP条件来循环过滤结果集。

这将减少您对数据库的查询量,并回复PHP脚本以过滤/获取适当的数据。