duanbiaojin8860 2018-07-20 04:23
浏览 47
已采纳

使用公共列从两个表中选择all - 相同的列名

table banners

| id | src       | roll |w  |...|
-------------------------
| 1  | blue.jpg  | 7    |120|...|
| 2  | white.jpg | 5    |250|...|
| 3  | green.jpg |      |140|...|

table brolls

| id | src        |...|
-------------------
| 4  | jones.jpg  |...|
| 5  | abba.jpg   |...|
| 7  | italia.jpg |...|

I need to select all from both tables

tables have different number of columns

not all rows from banners have a roll value

banners.roll is in fact brolls.id

while ($row = $st->fetch()){
    $items .=
    "<img src = '" . $row['banners.src'] . "'
    data-id = " . $row['banners.id'] . "
    data-roll = '" . $row['brolls.src'] . "'
    data-w = " . $row['banners.w'] . "
    alt='img'>

";
}

So final result should be:

<img src = 'blue.jpg' data-id=1 data-roll='italia.jpg' data-w=120 alt='img'>

I need all images from banners listed this way.

Any help?

  • 写回答

1条回答 默认 最新

  • douping1581 2018-07-20 04:44
    关注

    You do not specify what type of database you have. I guess you are using mysql. You could be clearer on saying what you want. I guess you are looking for the SQL queries and the PHP code to sort the results the way you want.

    Here is a suggestion.

    The SQL must be:

    $sql_a = 'SELECT id, src, roll, w FROM banners';
    $sql_b = 'SELECT id, src FROM brolls';
    

    You should get all of them in 2 different queries, and form 2 different arrays, one for each: $array_banners and $array_brolls; with the next structure:

    $array_banners['id value'] = array('src' => 'src value', 'roll' => 'roll value', 'w' => 'w value')

    $array_brolls['id value'] = 'src value';

    Then you will do the image html coding:

    foreach( $array_banners as $k => $v)
    {
    "<img src = '" . $v['src'] . "'
        data-id = " . $k . "
        data-roll = '" . $array_brolls[$v['roll']]['src'] . "'
        data-w = " . $v['w'] . "
        alt='img'>
    
    "
    };
    

    And ready.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值