简体   繁体   English

内部联接选择语句

[英]Inner join select statement

the SQL statement SQL语句

$CustInfo = Select * from wp_wpsc_submited_form_data where log_id = '6';

results in the following: 结果如下:

ID    CUST_ID    FORM_ID    VALUE

81       6          2       John
82       6          3       Smith
83       6          4       123 Main Street
84       6          5       Houston
96       6          6       NULL
85       6          7       US
86       6          8       77459

I have been scratching my head for a few hours with all the example here on Stack Overflow, but can't see to find anything close enough that is understandable. 我已经花了几个小时来研究Stack Overflow上的所有示例,但是看不到任何可以理解的足够接近的地方。 I am hoping someone can plainly and simply explain and may be provide an example as to how I can generate an HTML table with the values. 我希望有人能简单明了地解释一下,并可能提供一个示例,说明如何生成带有值的HTML表。

example: 例:

Firstname :   <%php echo $CustInfo[2]; ?>
Lastname :    <%php echo $CustInfo[3]; ?>
Address :    <%php echo $CustInfo[4]; ?>

etc... 等等...

How about something like this: (untested) 这样的事情怎么样:(未经测试)

if(is_array($CustInfo) && count($CustInfo) > 0)
{
  echo "<table>";
  for($i=0;$i<count($CustInfo);$i++)
  {
     echo "<tr>";
     switch($i)
     {
       case 1: echo "<td>FirstName:</td><td>".$CustInfo[i]."</td>";
       ...
     }
     echo "</tr>";
  }
  echo "</table>";
}

I check to see if the query returned a result with is_array and count() then iterator through the array using a switch/case to determine what the label to use. 我检查查询是否返回了带有is_array和count()的结果,然后使用开关/大小写来遍历数组以确定要使用的标签。 Just add case 2 to 8 and this should do the trick. 只需将情况2添加到8,就可以解决问题。

EDIT: An alternative would be to use a variable to the store the HTML and echo it after the process has completed. 编辑:一种替代方法是使用一个变量来存储HTML,并在过程完成后回显它。

This nasty bit of code should do the trick, it uses a group_concat to group non null rows into a single field nd as the unions mean only one column is null for each row, it should work: 这个讨厌的代码应该可以解决这个问题,它使用group_concat将非空行分组为单个字段nd,因为并集意味着每一行只有一列为空,它应该可以工作:

select 
    group_concat(Firstname) as FirstName,
    group_concat(Surname) as Surname,
    group_concat(Add1) as Add1,
    group_concat(Add2) as Add2,
    group_concat(Add3) as Add3,
    group_concat(Country) as Country,
    group_concat(ZipCode) as ZipCode
from
(
    select
        value as FirstName,
        null as Surname,
        null as Add1,
        null as Add2,
        null as Add3,
        null as Country,
        null as ZipCode
    from 
        wp_wpsc_submited_form_data 
    where 
        log_id = '6' // I can't actually see this field in your columns??
        and form_id=2
    union
    select
        null as FirstName,
        Value as Surname,
        null as Add1,
        null as Add2,
        null as Add3,
        null as Country,
        null as ZipCode
    from 
        wp_wpsc_submited_form_data 
    where 
        log_id = '6'
        and form_id=3
    union
    ...
    ...
    ...
    union
    select
        null as FirstName,
        null as Surname,
        null as Add1,
        null as Add2,
        null as Add3,
        null as Country,
        Value as ZipCode
    from 
        wp_wpsc_submited_form_data 
    where 
        log_id = '6'
        and form_id=8
)

Then you should be able to iterate nicely through the rows and do this: 然后,您应该能够很好地遍历各行并执行以下操作:

Firstname :   <%php echo $CustInfo['FirstName']; ?>

And so on. 等等。

I also wrote a length Q&A you might be interested in here: How can an SQL query return data from multiple tables 我还写了一个长度Q&A,您可能对此感兴趣: SQL查询如何从多个表返回数据

Edit: I see that the other two folks who answered told you to redesign your table - which I sort of do, but sort of don't agree with. 编辑:我看到回答的其他两个人告诉您重新设计桌子-我有点这样做,但有点不同意。

This table is ruggedly normalized , which means you can add a hundred extra form fields and not have to touch the table itself - that's a (amazingly) good thing. 该表经过严格的规范化处理 ,这意味着您可以添加一百个额外的表单字段,而不必触摸表本身-这是一件(令人惊奇的)好事。 I would say that this design is making it harder for you to get this particuar data out, but it very robust - if not amazingly quick or easy to access. 我要说的是,这种设计使您很难获得这些特殊的数据,但是它非常健壮-如果不是那么快或容易访问的话。

The issue is the database design, not the PHP or SQL. 问题是数据库设计,而不是PHP或SQL。

Your table should look like this: 您的表应如下所示:

| CUST_ID (PK) |  FIRSTNAME | LASTNAME | STREETADDRESS   | CITY    | STATE | ZIPCODE |
 ------------------------------------------------------------------------------------
| 6            | John       | Smith    | 123 Main Street | Houston | NULL  | 77459   |
| 7            | Jane       | Doe      | 456 Fake St.    | Richmond| VA    | 23860   |

From here, you can call these by simply using the SQL Statement: 从这里,您可以使用SQL语句简单地调用它们:

SELECT *
FROM UserTable
WHERE CUST_ID = 6

This will return you a simple answer, which you can use like so: 这将为您返回一个简单的答案,您可以像这样使用它:

FirstName: <%php echo $CustInfo['FIRSTNAME']; ?>

(Disclaimer: I have limited PHP knowledge, but I think this will work. If not, I do know it's not much more complicated) (免责声明:我对PHP的知识有限,但是我认为这可以工作。如果没有,我知道它并没有那么复杂)

Much much simpler, much less code... and most importantly imho, MUCH more efficient. 更简单,更少代码...而且最重要的是恕我直言,更有效。

I do suggest you read up on Database Normalization for better understanding on how Databases should be designed. 我确实建议您阅读有关数据库规范化的内容,以更好地了解应如何设计数据库。 But it's not needed for this, just something good to know and understand. 但这并不需要,只是一些很好的了解和理解。

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

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