# Retrieve multiple rows from mysql and automatically create a table

**URL:** <https://community.unix.com/t/retrieve-multiple-rows-from-mysql-and-automatically-create-a-table/330461>\
**Category:** Shell Programming and Scripting\
**Created:** [June 12, 2013, 11:47pm UTC](https://community.unix.com/t/retrieve-multiple-rows-from-mysql-and-automatically-create-a-table/330461 "2013-06-12T23:47:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jazzyzha](https://community.unix.com/letter_avatar/jazzyzha/32/5_5575768a8748004e209b776fc1b2916d.png) [@jazzyzha](https://community.unix.com/u/jazzyzha)\
**Post date:** [June 12, 2013, 11:47pm UTC](https://community.unix.com/t/retrieve-multiple-rows-from-mysql-and-automatically-create-a-table/330461/1 "2013-06-12T23:47:51Z")

</div>

Hi, i want to create a table automatically based on another table (sms\_key).  
For example;

If user create a new row with sms\_keyword field: IRC then a table created automatically (with some field on it, like: name, ph\_number, messages).

```nohighlight
select * from sms_key;
+-------------+
| sms_keyword |
+-------------+
| IRC |
| PTM |
| ADM |
| BS |
+-------------+

```

```nohighlight
select * from IRC;
+------+-----------------------------+
| x | Name | ph_number | messages |
+------+-----------------------------+
| 1 | | | |
+----+-------+-----------+----------+

```

can I do that in mysql?  
thank you

---

<div class="post-metadata">

**Author:** ![DGPickett](https://community.unix.com/letter_avatar/dgpickett/32/5_5575768a8748004e209b776fc1b2916d.png) [@DGPickett](https://community.unix.com/u/DGPickett)\
**Post date:** [June 13, 2013, 1:09pm UTC](https://community.unix.com/t/retrieve-multiple-rows-from-mysql-and-automatically-create-a-table/330461/2 "2013-06-13T13:09:53Z")

</div>

There are two obvious ways, either put the table HTML in the SQL string generation, or postprocess the SQL output with sed or the like. I usually do the latter:

```nohighlight
echo '<TABLE>'
 
echo "SELECT 'dAtA' x, t.* from some_table t order by 1,2,3,4 ;" | mysql ... | sed '
  s/^| dAtA | /<TR><TD>/
  t good
  w logfile
  d
  :good
  s/ *|$//
  s/ *| */<TD>/g
 '
 
echo '</TABLE>'

```
