在其他表的帮助下选择mySQL数据

时间:2015-05-18 17:53:27

标签: php mysql sql select

我目前正在制作留言板,只有在设置了另一个表中的值时才会显示数据。

我的2个表格如下:

tbsubscribers:
-subID (int)
-msg1 (tinyint)
-msg2 (tinyint)
-msg3 (tinyint)
-email (varchar)

tbmessage
-id (int)
-name (varchar)
-mitteilung (varchar)
-sender (varchar)
-datum (datetime)

实际消息位于tbmessage表中,并检查用户想要查看的消息是否存在tbsubscribers表。

现在,我想显示消息。因此,如果用户订阅msg1msg2(因此两者的值均为1),则应选择来自tbmessage的所有数据,其名称为'msg1''msg2'

我知道如何使用mysql中的select,但我不知道如何选择多个表格,特别是在这种情况下,我有点困惑我会这样做。

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

简单的连接应该有效:

<?php

/*
 * If you already have username (email) in session,
 * then you can do this:
 */

$query = "SELECT m.* FROM `tbsubscribers` s
RIGHT JOIN `tbmessage` m ON (
  CASE WHEN msg1 = 1 THEN name = 'msg1' END OR
  CASE WHEN msg2 = 1 THEN name = 'msg2' END OR
  CASE WHEN msg3 = 1 THEN name = 'msg3' END
)
WHERE email = " . my_escape_string_function($_SESSION['username']);

/*
 * If tbsubscribers has a '1' in msg1, then all msg1's are grabbed.
 * If tbsubscribers has a '2' in msg2, then all msg2's are grabbed...etc
 */

//Set $rows equal to the result

return $rows;

?>

我创建了一个sqlfiddle here,它应该展示您正在寻找的功能。

我将进行一些编辑,以澄清你应该如何使用上面的查询和另一个查询(如果需要)。

有几种方法可以做到这一点。方式#1:

<?php

/*
 * If you already have username (email) in session,
 * then you can do this:
 */

$query = "SELECT * FROM `tbsubscribers` s
WHERE email = " . my_escape_string_function($_SESSION['username']);

/*
 * Run this query, and fetch the entire userrow, and put into $user
 * An alternative could be to just store the entire user array into
 * $_SESSION, as you wont have to query the database twice.
 */

$tofetch = array();

for ( $i=1; $i<=3; $i++ )
{
    //Let's check msg1, msg2, msg3
    if ( $user['msg' . $i] )
    {
        $tofetch[] = "name = 'msg" . $i . "'";
    }
}

/*
 * If tbsubscribers has a '1' in msg1, and a '1' in msg2 then the array
 * will look like this:
 * array(
 * 0 => "name = 'msg1'"
 * 1 => "name = 'msg2'"
 * )
 */

//Throw an exception if all 'msg1', 'msg2', and 'msg3' are all 0 (Otherwise the query will fail)
if ( empty($tofetch) ) {
    throw new Exception("You're not subscribed to any messages.");
}

/*
 * Now it's a simple query. $tofetch will be imploded, to form a nice
 * where statement. Using the example above, it will look like:
 * name = 'msg1' OR name = 'msg2'
 */
$query = "SELECT *
FROM `tbmessage`
WHERE " . implode(' OR ', $tofetch);

//Set $rows equal to the result

return $rows;

方式#2:

<asp:Chart  ID="Chart1" runat="server" Width="900px" Height="600px" BorderSkin-SkinStyle="FrameTitle1" BorderSkin-BackColor="White">
            <Titles>
                <asp:Title></asp:Title>
            </Titles>
            <Series>
                <asp:Series  IsValueShownAsLabel="true"  IsVisibleInLegend="true" LabelForeColor="Black" LabelFormat="N2"  Color="#F7FE2E"  >
                </asp:Series>
            </Series>
            <Series>
                <asp:Series IsValueShownAsLabel="true" IsVisibleInLegend="true" LabelForeColor="Black" LabelFormat="N2">
                </asp:Series>
            </Series>              
            <Series>
                <asp:Series IsValueShownAsLabel="true" IsVisibleInLegend="true" LabelForeColor="Black" LabelFormat="N2" Color="YellowGreen">
                </asp:Series>
            </Series>
            <Legends>  
                <asp:Legend   
                    Name="Legend1"  
                    BackColor="White" 
                    ForeColor="Black"  
                    BorderColor="White" 
                    Docking="Bottom">  
                </asp:Legend>  
            </Legends>
            <ChartAreas>
                <asp:ChartArea Name="ChartArea1">
                </asp:ChartArea>
            </ChartAreas>
        </asp:Chart>