简体   繁体   English

PHP,MySQL,SELECT问题

[英]PHP, MySQL, SELECT question

Hello I have a question that could seem complicated. 您好,我有一个看似复杂的问题。 But I will try to explain it as better as I can. 但我会尽力解释。 This is a problem of logic. 这是逻辑问题。 I have a database table. 我有一个数据库表。 This database table (lets call it Table A) contains some strings. 该数据库表(称为表A)包含一些字符串。 Strings are composed by these rows: 字符串由以下各行组成:

ID (auto increment)
Text
Date 
Time
Type
IDAccount.

Now some of these strings are equal. 现在,其中一些字符串是相等的。 I need to SELECT all the strings that are equal and have them in one result. 我需要选择所有相等的字符串,并将它们放在一个结果中。

So the logic of the code will be: 因此,代码的逻辑将是:

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

Then "echo" for only ONE TIME(so not for any string, since they are perfectly equal): Text Date Time. 然后“回显”仅一次(因此,对于任何字符串均不相等,因为它们完全相等):文本日期时间。 (this will be an HTML DIV) (这将是HTML DIV)

Then in these strings we have some other rows that have DIFFERENT values. 然后,在这些字符串中,我们还有一些其他行具有不同的值。

Type
IDAccount.

The PHP will have to show for these rows(Type, IDAccount) all the different values found for the EQUAL strings above. PHP必须为这些行(类型,IDAccount)显示为上面的EQUAL字符串找到的所有不同值。

So the final DIV would be: 因此,最终的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).

I know this is something difficult to understand. 我知道这很难理解。 I hope someone got my point. 我希望有人明白我的意思。 The most important thing is "how to say to mySQL to SELECT all the strings that are equale for rows Text , Date and Time and show 1 result in the final DIV, and then how to show the not equal values for all those strings (Type, IDAccount) and show all of them in the same final DIV". 最重要的是“如何对mySQL说出要选择与文本,日期和时间行相等的所有字符串并在最终DIV中显示1结果,然后如何显示所有这些字符串的不相等值(类型,IDAccount),并在同一最终DIV中显示所有这些信息。”。

Any help would be very appreciated!! 任何帮助将不胜感激!

Lets play with it =D 让我们一起玩= D

I think the best way to do this would be in the PHP code, rather than in SQL. 我认为最好的方法是在PHP代码中,而不是SQL中。

You can achieve this by simply creating an associative array in PHP, using the "text" field as a key, that contains the data you want - and populating it as you pull information from the database. 您可以通过简单地在PHP中创建一个关联数组(使用“文本”字段作为键)来实现此目的,该数组包含所需的数据,并在您从数据库中提取信息时填充它。

An example: 一个例子:

SQL: SELECT * FROM myTable SQL: SELECT * FROM myTable

PHP Code: 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'];
}

?>

This will give you an array as follows: 这将为您提供如下数组:

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

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

and so on. 等等。

I hope that's helpful. 希望对您有所帮助。

EDIT: Poster asked for help with display. 编辑:海报寻求显示帮助。

<?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']);
}

/* Alternatively, you can loop each array contained in $info if you need more control */ / *或者,如果需要更多控制,则可以循环$ 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 . ' - ';
    }
}

Do you mean the following : 您的意思是:

Row 1 : 第1行:

Text Date Time ( once )

- type ,IDAccount ( multiple ).

You probably did not set up your tables right. 您可能没有正确设置表格。

You should use 2 tables , so you will not get duplicated rows. 您应该使用2个表,这样您就不会得到重复的行。

TABLE 1 :
  id
  text
  time

Table 2
  type
  IDaccount
  linkid

Then select all from table 1 and get in a second query every rows linked to table 2 然后从表1中全选,并在第二个查询中查询链接到表2的每行

Two separate problems in the logic of what you propose. 您提出的逻辑中存在两个独立的问题。

First, if I understand correctly, there are groups of data repeating in your table. 首先,如果我理解正确,则表中有重复的数据组。 If so, you should break up your data to avoid repetition - as it is called "normalise" the data. 如果是这样,则应该分解数据以避免重复-这被称为“标准化”数据。 Something like this: 像这样:

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

This way the tables have no repeated data and none of the associated risks of error (eg data that should be the same but isn't because of typos etc.) 这样,表格就没有重复的数据,也没有相关的错误风险(例如,数据应该相同,但不是由于错别字等)。

Second, you could use PHP constructs to obtain what you want. 其次,您可以使用PHP构造来获取所需的内容。 How you do it depends on exactly what you want, and possibly on the number of rows in tables 1, and 2. If table 2 has few rows I would do this (pseudo code): 如何执行取决于您想要的内容,并且可能取决于表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

However, if there are many rows in table2 and usually just one or two corresponding rows in table 1 I would do that: 但是,如果表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