MySQL多表数据选择

时间:2016-09-18 21:27:42

标签: mysql join distinct union

我有两张桌子我试图加入,第一张桌子存储了与高尔夫球手相关的信息以及他们在各种锦标赛中得到的分数(每个锦标赛的新表,所有相同的结构),第二桌上有很多用户和他们为每场锦标赛挑选的5位高尔夫球手:

表1(MField):

+---------------+------+------+----+------+------+
| MGolf         | Rd1P | Rd2P | CP | Rd3P | Rd4P |
| Jason Day     | 0    | -1   | 5  | 2    | -1   |
| Jordan Spieth | 6    | -2   | 5  | -1   | -1   |
| Rory McIlroy  | 2    | 1    | 5  | -5   | 3    |
+---------------+------+------+----+------+------+

表2(名册):

+-------+---------------+
| User  | GName         |
| User1 | Jason Day     |
| User1 | Jordan Spieth |
| User1 | Rory McIlroy  |
+-------+---------------+

我理想的输出是:

+---------------+------+------+----+------+------+
| User1         | Rd1P | Rd2P | CP | Rd3P | Rd4P |
| Jason Day     | 0    | -1   | 5  | 2    | -1   |
| Jordan Spieth | 6    | -2   | 5  | -1   | -1   |
| Rory McIlroy  | 2    | 1    | 5  | -5   | 3    |
+---------------+------+------+----+------+------+

我所看到的是:

+---------------+-----+-----+-----+-----+-----+
| User1         | Rd1 | Rd2 | Cut | Rd3 | Rd4 |
| Jason Day     | 0   | -1  | 5   | 2   | -1  |
| User1         | Rd1 | Rd2 | Cut | Rd3 | Rd4 |
| Jordan Spieth | 6   | -2  | 5   | -1  | -1  |
| User1         | Rd1 | Rd2 | Cut | Rd3 | Rd4 |
| Rory McIlroy  | 2   | 1   | 5   | -5  | 3   |
+---------------+-----+-----+-----+-----+-----+

我使用以下查询:

$sql = "
SELECT 
    User,
    GName,
    MGolf,  
    Rd1P,
    Rd2P,
    CP,
    Rd3P,
    Rd4P
FROM 
    Rosters, 
    MField
WHERE 
    roster.GName = MGolf 
";

$result = $link->query($sql);

echo '<table>';

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo '<tr><td>' . $row['User'] . '</td><td>Rd1</td><td>Rd2</td><td>Cut</td><td>Rd3</td><td>Rd4</td></tr>';
        echo '<tr><td>' . $row['GName'] . '</td><td>'.$row['Rd1P'].'</td><td>'.$row['Rd2P'].'</td><td>'.$row['CP'].'</td><td>'.$row['Rd3P'].'</td><td>'.$row['Rd4P'].'</td></tr>'; 
    }
} else {
    echo "0 results";
}
echo '</table>';

我遇到的问题是我只想向“用户”展示这个问题。命名一次,但它目前正在交替表格中的每一行。我知道这是因为查询的结构如何,但我无法弄清楚如何显示“用户”#39;有一次,在他们下方的5个高尔夫球手的行中,然后是来自“MField”的高尔夫球手信息。表。有没有更简洁的方法来组合这些信息?

1 个答案:

答案 0 :(得分:0)

在循环中,只打印一次标题行。使用变量来跟踪它。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd

    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.2.xsd">

    <context:annotation-config />
    <context:component-scan base-package="org.tuto" />

    <!-- bean definitions go here -->

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/****" />
        <property name="username" value="****" />
        <property name="password" value="****" />
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <constructor-arg ref="dataSource" />
    </bean>

    <bean id="userService" class="org.tuto.service.UserServiceImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate" />
    </bean>

</beans>