PHP、MySQL、SELECT 问题
您好,我有一个看起来很复杂的问题。但我会尽力解释得更好。这是一个逻辑问题。 我有一个数据库表。该数据库表(我们称之为表 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为最好的方法是在 PHP 代码中,而不是在 SQL 中。
您可以通过简单地在 PHP 中创建一个关联数组,使用“文本”字段作为键来实现此目的,其中包含您想要的数据 - 并在从数据库中提取信息时填充它。
一个例子:
SQL:
SELECT * FROM myTable
PHP 代码:
这将为您提供一个数组,如下所示:
等等。
我希望这有帮助。
编辑:海报请求有关显示的帮助。
/* 或者,如果需要更多控制,您可以循环 $info 中包含的每个数组 */
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:
This will give you an array as follows:
and so on.
I hope that's helpful.
EDIT: Poster asked for help with display.
/* Alternatively, you can loop each array contained in $info if you need more control */
您的意思是:
第 1 行:
您可能没有正确设置表格。
您应该使用 2 个表,这样您就不会得到重复的行。
然后从表 1 中选择所有内容,并在第二个查询中获取链接到表 2 的每一行
Do you mean the following :
Row 1 :
You probably did not set up your tables right.
You should use 2 tables , so you will not get duplicated rows.
Then select all from table 1 and get in a second query every rows linked to table 2
您提出的逻辑中有两个独立的问题。
首先,如果我理解正确的话,您的表中存在重复的数据组。如果是这样,您应该分解数据以避免重复 - 因为这称为“标准化”数据。像这样的事情:
这样,表就没有重复的数据,也没有相关的错误风险(例如,数据应该相同,但由于拼写错误等原因并非如此)。
其次,您可以使用 PHP 构造来获取您想要的内容。如何执行此操作完全取决于您想要的内容,并且可能取决于表 1 和表 2 中的行数。如果表 2 的行数很少,我会这样做(伪代码):
但是,如果表 2 和表 2 中有很多行,通常只有表 1 中的一两个对应行,我会这样做:
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:
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):
However, if there are many rows in table2 and usually just one or two corresponding rows in table 1 I would do that: