Jithin Varghese
Jithin Varghese

Reputation: 2228

Merging 2 mysql table at specified positions

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.

Upvotes: 1

Views: 36

Answers (1)

Devart
Devart

Reputation: 122002

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
...

Upvotes: 1

Related Questions