PHP、MySQL、SELECT 问题

发布于 2024-11-07 11:38:00 字数 948 浏览 0 评论 0原文

您好,我有一个看起来很复杂的问题。但我会尽力解释得更好。这是一个逻辑问题。 我有一个数据库表。该数据库表(我们称之为表 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 说 SELECT 所有与行 Text 、 Date 和 Time 相等的字符串并在最终 DIV 中显示 1 个结果,然后如何显示所有这些字符串的不相等值(Type 、IDAccount)并在同一个最终 DIV 中显示所有这些”。

任何帮助将不胜感激!

来玩玩吧=D

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.
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)

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.

So the final DIV would be:

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".

Any help would be very appreciated!!

Lets play with it =D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三人与歌 2024-11-14 11:38:00

我认为最好的方法是在 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 . ' - ';
    }
}

I think the best way to do this would be in the PHP code, rather than in 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.

An example:

SQL:
SELECT * FROM myTable

PHP Code:

<?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 */

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 . ' - ';
    }
}
回忆躺在深渊里 2024-11-14 11:38:00

您的意思是:

第 1 行:

Text Date Time ( once )

- type ,IDAccount ( multiple ).

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

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

TABLE 1 :
  id
  text
  time

Table 2
  type
  IDaccount
  linkid

然后从表 1 中选择所有内容,并在第二个查询中获取链接到表 2 的每一行

Do you mean the following :

Row 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.

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

撩起发的微风 2024-11-14 11:38:00

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

首先,如果我理解正确的话,您的表中存在重复的数据组。如果是这样,您应该分解数据以避免重复 - 因为这称为“标准化”数据。像这样的事情:

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 和表 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

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. 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):

$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:

$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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文