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