PHP,MySQL,SELECT问题 [英] PHP, MySQL, SELECT question

查看:81
本文介绍了PHP,MySQL,SELECT问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一个可能看起来很复杂的问题。但是我会尽可能地更好地解释它。这是一个逻辑问题。
我有一个数据库表。这个数据库表(让我们称之为表A)包含一些字符串。
字符串由这些行组成:

  ID(自动增量)
文本
日期
时间
键入
IDAccount。

现在有些字符串是相同的。我需要选择所有相同的字符串,并在一个结果中。

所以代码的逻辑将是:

 从表A中选择* WHERE mySQL查找具有相同文本,日期和时间的字符串。 

然后,回声只有一次(所以不适用于任何字符串,因为它们是完全相等的) :文本日期时间。 (这将是一个HTML DIV)



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

 键入
IDAccount。

PHP将为这些行(Type,IDAccount)显示所有不同的值等于上面的字符串。

所以最后的DIV是:

pre $ $ $ $ $文本(一次)
日期(一次)
时间(一次)
类型(每个不同的值被发现将被显示)
IDAccount(每个不同的值是找到将显示)。

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

任何帮助将非常赞赏!!



让我们玩吧= D

解决方案

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



您可以通过在PHP中创建一个关联数组来实现这一点,使用text字段作为关键字,包含数据你需要 - 当你从数据库中提取信息时填充它。



一个例子:
$ b $ p

SQL:
SELECT * FROM myTable





 <?php 

//连接到MySQL数据库
$ 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'

等。



我希望这是有帮助的。



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



<$ p $ ($ stringsInfo as $ string => $ info)
{
echo $ string $ <?php

foreach '< br />';
echo'Types:'。 implode(',',$ info ['types']); //这将回显用逗号分隔的每个类型
echo'< br />';
echo'ID Accounts:'。 implode(',',$ info ['idAccounts']);





$ p $ / b另外,如果你需要,你可以循环$ 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。 ' - ';
}
}


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

解决方案

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 . ' - ';
    }
}

这篇关于PHP,MySQL,SELECT问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆