简体   繁体   English

SQL查询,我需要获取一些值

[英]SQL queries, I need to get some values

I got 2 tables, table1 & table2. 我有2个表,table1和table2。

table1 have fields: table1有字段:

id
atype
adesc
aid

table2 have fields: table2有字段:

id
aid
adesc
value_1
value_2


$query1 = mysql_query("Select DISTINCT atype from table1");
while($row = mysql_fetch_array($query1)){
    $atype = $row['atype'];
    $query2 = mysql_query("Select adesc from table1 where atype='$atype' and aid IN (Select aid from table2 ) order by id asc");
    while($row2 = mysql_fetch_array($query2)){
        // i know query2 can only get adesc, so i need value_1 and value_2 in this
        echo $row2['adesc'] .'>> '. (this should be value1 from table2) .'>> '. (this should be value2 from table2);
    }
}


I want to get the value_1 and value_2 too. 我也希望获得value_1value_2 Any help would be appreciated. 任何帮助,将不胜感激。




EDIT: 编辑:
table1 values are in my database (atype, aid, adesc respectively): table1值在我的数据库中(atype,aid,adesc):

type1 111 'this is type 1'
type1 111 'this is type 1'
type2 112 'this is type 2'
type3 113 'this is type 3'
type4 114 'this is type 4'
type1 111 'this is type 1'
type4 114 'this is type 4'
type2 112 'this is type 2'

values on my table2 (aid,adesc,val1,val2, respectively): 我的table2上的值(辅助,adesc,val1,val2):

111  'this is type 1' 100 50
111  'this is type 1' 100 50
112  'this is type 2' 300 500
113  'this is type 3' 100 50
112  'this is type 2' 100 50
114  'this is type 4' 100 50
111  'this is type 1' 100 50

what i really want to project is this: 我真正想要的是这个:

type1
      (sum)value_1 (sum)value_2
type2 
      (sum)value_1 (sum)value_2
type3
      (sum)value_1 (sum)value_2
type4
      (sum)value_1 (sum)value_2
    $query2 = mysql_query("Select t1.adesc, t2.value_1, t2.value_2 from table1 as t1, table2 as t2 where t1.atype='$atype' and t1.aid = t2.aid order by t1.id asc");

    while($row2 = mysql_fetch_array($query2)){
        // i know query2 can only get adesc, so i need value_1 and value_2 in this
        echo $row2['adesc'] .'>> '. $row2['value_1'] .'>> '. $row2['value_2'];
    }

upd: UPD:

$sql = <<<SQL
select t1.adesc, sum(t2.value_1) as v1, sum(t2.value_2)  as v2
from 
table1 as t1,
table2 as t2

where 
t1.aid = t2.aid 
group by t1.atype

order by t1.atype asc
SQL;
$query = mysql_query($sql);
while($row = mysql_fetch_array($query2)){
    // i know query2 can only get adesc, so i need value_1 and value_2 in this
        echo $row['adesc'] .'>> '. $row['v1']  .'>> '. $row['v2'];
}

Use one simple innerjoin in SQL, instead of your two selects: 在SQL中使用一个简单的innerjoin,而不是两个选择:

select table1.adesc,atable2.value1 from table1,table2 where table2.adesc=table1.adesc and table1.aid=table2.aid order by table1.aid asc

Edit: 编辑:

select table1.adesc,atable2.value1 from table1,table2 where table1.aid=table2.aid order by table1.aid asc

This SQL query should produce the results you want: 此SQL查询应生成所需的结果:

select adesc, 
       value_1, 
       value_2 

from   table1 

where  atype='$atype' and 
       aid IN (Select aid from table2 ) 

order by id asc

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

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