简体   繁体   English

在PHP + MySql中关联表

[英]Associating tables in PHP + MySql

So, i'm creating a money management system. 因此,我正在创建一个资金管理系统。 My database has 2 tables: main(id,timestamp,value1,value2) and fields(value1,value2). 我的数据库有2个表:main(id,timestamp,value1,value2)和fields(value1,value2)。

Value1 and value2 are the categories in which the money are spent. 值1和值2是花钱的类别。 In fact, the script display a table where the rows are the days and the columns are the fields where the money have been spent into (sport, work, girlfriend, etc). 实际上,脚本会显示一个表格,其中的行是天,而列的是花了钱的字段(体育,工作,女友等)。

Now, i have a problem. 现在,我有一个问题。 The main query is this: 主要查询是这样的:

SELECT * FROM main WHERE FROM_UNIXTIME(timestamp, '%M %Y') = '$date' ORDER BY timestamp ASC

as you see, i extract data only from the main table. 如您所见,我仅从主表中提取数据。 So if i wanna list all the categories contents i have to write manually the category name for each one of them, like this: 因此,如果我想列出所有类别的内容,则必须手动为每个类别编写类别名称,如下所示:

echo "<td>".$row['value'] ."€</td>";
echo "<td>".$row['value2'] ."€</td>";

Is there any way it could list all the category name (showing the category content) automatically? 有什么办法可以自动列出所有类别名称(显示类别内容)?

Maybe you just need to cycle through the $row array? 也许您只需要循环$ row数组?

foreach ($row as $content) {
    echo "<td>$content €</td>"; 
}

I think I understood what you want... you want to group your expendings by category, correct? 我想我了解您想要的...您想按类别对支出进行分组,对吗? I didn't understand how is the relation is done on your two tables so I can't help on you on the SQL statement you are looking for, you need to explain it better, but what you are looking for are something very special in SQL, called JOIN s. 我不了解如何在您的两个表上建立关系,所以我无法在您要查找的SQL语句上为您提供帮助,您需要对其进行更好的解释,但是您所寻找的是SQL,称为JOIN

Here is a good website with examples: http://www.keithjbrown.co.uk/vworks/mysql/mysql_p5.php 这是一个带有示例的好网站: http : //www.keithjbrown.co.uk/vworks/mysql/mysql_p5.php

Hope I've helped 希望我有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM