douyi6922 2015-08-17 05:58
浏览 8
已采纳

在指定位置合并2个mysql表

I have 2 mysql tables as listed below,

//First table (db_event)

id  ||   name  ||  publish

1         a          1
2         b          1
3         c          1
4         d          1
5         e          1
6         f          1

//Second table (db_ads)

id  ||  images  ||  publish

1        a.jpg        1
2        b.jpg        1

I want to merge the second table db_ads into first table db_event at specified position ie, at 5th position respectively.

my required result should look like below table,

//resulting table

id  ||  name  ||  images  ||  publish

1        a        {null}        1
2        b        {null}        1
3        c        {null}        1
4        d        {null}        1
5        e        {null}        1

1      {null}     a.jpg         1

6        f        {null}        1

2      {null}     b.jpg         1

Is there any method achieve this result in php or mysql. I don't need this results in JSON.

If I use,

while($event_rows = mysql_fetch_array($event))
{
     $name = $event_rows["name"];
     $image= $event_rows["images"];
}
echo $name;
echo $image;

The result should appear.

  • 写回答

1条回答 默认 最新

  • dongqiaogouk86049 2015-08-17 06:54
    关注

    I do not find it a good solution, but if your result depends on ID values, you may use something like this -

    SELECT id, name, NULL AS images, publish FROM db_event WHERE id < 6
    UNION
    SELECT id, NULL AS name, images, publish FROM db_ads WHERE id = 1
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度