繁体   English   中英

PHP,MySQL,SELECT问题

[英]PHP, MySQL, SELECT question

您好,我有一个看似复杂的问题。 但我会尽力解释。 这是逻辑问题。 我有一个数据库表。 该数据库表(称为表A)包含一些字符串。 字符串由以下各行组成:

ID (auto increment)
Text
Date 
Time
Type
IDAccount.

现在,其中一些字符串是相等的。 我需要选择所有相等的字符串,并将它们放在一个结果中。

因此,代码的逻辑将是:

SELECT * from Table A WHERE mySQL find stringS with same Text, Date and Time. 

然后“回显”仅一次(因此,对于任何字符串均不相等,因为它们完全相等):文本日期时间。 (这将是HTML DIV)

然后,在这些字符串中,我们还有一些其他行具有不同的值。

Type
IDAccount.

PHP必须为这些行(类型,IDAccount)显示为上面的EQUAL字符串找到的所有不同值。

因此,最终的DIV为:

Text (one time)
Date (one time)
Time (one time)
Type (every different value that is found will be shown)
IDAccount (every different value that is found will be shown).

我知道这很难理解。 我希望有人明白我的意思。 最重要的是“如何对mySQL说出要选择与文本,日期和时间行相等的所有字符串并在最终DIV中显示1结果,然后如何显示所有这些字符串的不相等值(类型,IDAccount),并在同一最终DIV中显示所有这些信息。”。

任何帮助将不胜感激!

让我们一起玩= D

我认为最好的方法是在PHP代码中,而不是SQL中。

您可以通过简单地在PHP中创建一个关联数组(使用“文本”字段作为键)来实现此目的,该数组包含所需的数据,并在您从数据库中提取信息时填充它。

一个例子:

SQL: SELECT * FROM myTable

PHP代码:

<?php

// Connect to MySQL database
$result = mysql_query($sql_query_noted_above);
$stringsInfo = array();
while ($row = mysql_fetch_assoc($result))
{
    if (!isset($stringsInfo[$row['text']]))
    {
        $stringsInfo[$row['text']] = array('types' => array(), 'idAccounts' => array());
    }

    $stringsInfo[$row['text']]['types'][] = $row['type'];
    $stringsInfo[$row['text']]['idAccounts'][] = $row['idAccount'];
}

?>

这将为您提供如下数组:

'myTextString' => 'types' => 'type1', 'type2', 'type3'
                  'idAccounts' => 'account1', 'account2'

'anotherTextString' => 'types' => 'type2', 'type4'
                       'idAccounts' => 'account2', 'account3'

等等。

希望对您有所帮助。

编辑:海报寻求显示帮助。

<?php

foreach ($stringsInfo as $string => $info)
{
    echo $string . '<br />';
    echo 'Types: ' . implode(', ', $info['types']); // This will echo each type separated by a comma
    echo '<br />';
    echo 'ID Accounts: ' . implode(', ', $info['idAccounts']);
}

/ *或者,如果需要更多控制,则可以循环$ info中包含的每个数组* /

foreach ($stringsInfo as $string => $info)
{
    echo $string . '<br />';
    echo 'Types: ';
    foreach ($info['types'] as $type)
    {
        echo $type . ' - ';
    }
    echo '<br />';
    echo 'ID Accounts: '
    foreach ($info['idAccounts'] as $idAccount)
    {
        echo $idAccount . ' - ';
    }
}

您的意思是:

第1行:

Text Date Time ( once )

- type ,IDAccount ( multiple ).

您可能没有正确设置表格。

您应该使用2个表,这样您就不会得到重复的行。

TABLE 1 :
  id
  text
  time

Table 2
  type
  IDaccount
  linkid

然后从表1中全选,并在第二个查询中查询链接到表2的每行

您提出的逻辑中存在两个独立的问题。

首先,如果我理解正确,则表中有重复的数据组。 如果是这样,则应该分解数据以避免重复-这被称为“标准化”数据。 像这样:

table 1                       table 2 (account?)                
================              ================
ID (primary key)              IDAccount (primary key)
Text                          Type
Date
Time
IDAccount (ref to table 2)

这样,表格就没有重复的数据,也没有相关的错误风险(例如,数据应该相同,但不是由于错别字等)。

其次,您可以使用PHP构造来获取所需的内容。 如何执行取决于您想要的内容,并且可能取决于表1和2中的行数。如果表2中的行很少,我会这样做(伪代码):

$query1 : "select * from table2";
for (each x = row in $query1)
   display IDAccount and type of x
   $ida = IDAccount of x
   query2 : "select * from table1 where IDAccount=$ida"
   for (each y = row in $query2) display y

但是,如果表2中有很多行,而表1中通常只有一两个相应的行,我会这样做:

$query : "select * from table1 join table2 on table1.IDAccount=table2.IDAccount"
$old_ida = -1
for (each x = row in $query)
   $ida = IDAccount of x
   if ($ida is different from $old_ida)
      show idaccount and type of $query
   show id, text, date, time of query
   $oldida = $ida

暂无
暂无

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

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