Prepend text to MySQL columns' names - mysql

Supposing a query such as:
SELECT * FROM tableA;
How can I prepend a_ to each columns' name? For example if there is a column "username" it would be accessed in the results as "a_username".
EDIT: The SELECT username AS a_username format will not help as I need to continue using the * field selection. There is a JOIN and a potential conflict with a returned column from another table in the JOIN. I will be iterating over the returned columns (foreach) and only want to output the columns that came from a particular table (whose schema may change) to HTML input fields where a site admin could edit the fields' content directly. The SQL query in question looks like SELECT firstTable.*, anotherTable.someField, anotherTable.someOtherField and their exists the possibility that someField or someOtherField exists in firstTable.
Thanks.

You can use the INFORMATION_SCHEMA.COLUMNS table to formulate the query and then use dynamic SQL to execute it.
First let's make a sample database called dotancohen and a table called mytable
mysql> drop database if exists dotancohen;
Query OK, 1 row affected (0.03 sec)
mysql> create database dotancohen;
Query OK, 1 row affected (0.00 sec)
mysql> use dotancohen
Database changed
mysql> create table mytable
-> (
-> id int not null auto_increment,
-> username varchar(30),
-> realname varchar(30),
-> primary key (id)
-> );
Query OK, 0 rows affected (0.06 sec)
mysql> insert into mytable (realname,username) values
-> ('rolando','odnalor'),('pamela','alemap'),
-> ('dominique','euqinimod'),('diamond','dnomaid');
Query OK, 4 rows affected (0.05 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from mytable;
+----+-----------+-----------+
| id | username | realname |
+----+-----------+-----------+
| 1 | odnalor | rolando |
| 2 | alemap | pamela |
| 3 | euqinimod | dominique |
| 4 | dnomaid | diamond |
+----+-----------+-----------+
4 rows in set (0.00 sec)
mysql>
Here is the metadata table called INFORMATION_SCHEMA.COLUMNS:
mysql> desc INFORMATION_SCHEMA.COLUMNS;
+--------------------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------------------+---------------------+------+-----+---------+-------+
| TABLE_CATALOG | varchar(512) | NO | | | |
| TABLE_SCHEMA | varchar(64) | NO | | | |
| TABLE_NAME | varchar(64) | NO | | | |
| COLUMN_NAME | varchar(64) | NO | | | |
| ORDINAL_POSITION | bigint(21) unsigned | NO | | 0 | |
| COLUMN_DEFAULT | longtext | YES | | NULL | |
| IS_NULLABLE | varchar(3) | NO | | | |
| DATA_TYPE | varchar(64) | NO | | | |
| CHARACTER_MAXIMUM_LENGTH | bigint(21) unsigned | YES | | NULL | |
| CHARACTER_OCTET_LENGTH | bigint(21) unsigned | YES | | NULL | |
| NUMERIC_PRECISION | bigint(21) unsigned | YES | | NULL | |
| NUMERIC_SCALE | bigint(21) unsigned | YES | | NULL | |
| CHARACTER_SET_NAME | varchar(32) | YES | | NULL | |
| COLLATION_NAME | varchar(32) | YES | | NULL | |
| COLUMN_TYPE | longtext | NO | | NULL | |
| COLUMN_KEY | varchar(3) | NO | | | |
| EXTRA | varchar(27) | NO | | | |
| PRIVILEGES | varchar(80) | NO | | | |
| COLUMN_COMMENT | varchar(1024) | NO | | | |
+--------------------------+---------------------+------+-----+---------+-------+
19 rows in set (0.02 sec)
mysql>
What you need from this table are the following columns:
table_schema
table_name
column_name
ordinal_position
What you are asking for is to have the column_name and the column_name prepended with a_
Here is the query and how to execute it:
select concat('select ',column_list,' from ',dbtb) into #newsql
from (select group_concat(concat(column_name,' a_',column_name)) column_list,
concat(table_schema,'.',table_name) dbtb from information_schema.columns
where table_schema = 'dotancohen' and table_name = 'mytable'
order by ordinal_position) A;
select #newsql;
prepare stmt from #newsql;
execute stmt;
deallocate prepare stmt;
Let's execute it
mysql> select concat('select ',column_list,' from ',dbtb) into #newsql
-> from (select group_concat(concat(column_name,' a_',column_name)) column_list,
-> concat(table_schema,'.',table_name) dbtb from information_schema.columns
-> where table_schema = 'dotancohen' and table_name = 'mytable'
-> order by ordinal_position) A;
Query OK, 1 row affected (0.01 sec)
mysql> select #newsql;
+--------------------------------------------------------------------------------+
| #newsql |
+--------------------------------------------------------------------------------+
| select id a_id,username a_username,realname a_realname from dotancohen.mytable |
+--------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> prepare stmt from #newsql;
Query OK, 0 rows affected (0.00 sec)
Statement prepared
mysql> execute stmt;
+------+------------+------------+
| a_id | a_username | a_realname |
+------+------------+------------+
| 1 | odnalor | rolando |
| 2 | alemap | pamela |
| 3 | euqinimod | dominique |
| 4 | dnomaid | diamond |
+------+------------+------------+
4 rows in set (0.01 sec)
mysql> deallocate prepare stmt;
Query OK, 0 rows affected (0.00 sec)
mysql>
Give it a Try !!!
You mentioned in your question : The SELECT username AS a_username format will not help as I need to continue using the * field selection.
All you have to do to implement my suggestion is run the query using tableA as follows:
select concat('select ',column_list,' from ',dbtb) into #newsql
from (select group_concat(concat(column_name,' a_',column_name)) column_list,
concat(table_schema,'.',table_name) dbtb from information_schema.columns
where table_schema = DATABASE() and table_name = 'tableA'
order by ordinal_position) A;
When you retrieve that query result, just use it as the query to submit to mysql_query.

You'll need to list the columns, e.g
SELECT username AS a_username FROM tableA;
alternatively, post-process in back-end, e.g. change the array keys in your code

Create a view with renamed columns, e.g. -
CREATE VIEW a_view AS SELECT username AS a_username FROM table;
Then refer to this view.

As already mentioned, there is no standard way to mass-prefix column names in a regular query.
But if you really wanted to achieve it, you could write a stored procedure which would query information_schema to get a list of columns in a table, and then prefix them one by one. After that it's possible to concatenate a query as string, PREPARE and EXECUTE it.
A downside to this approach is the fact that you cannot join on a stored procedure's result. But of course, you could as well create a stored procedure for each type of query you issue. Prefixing the fields for any table could then be made a separate generic FUNCTION.
All of this stuff sounds to me like an overkill, though. I would recommend either renaming the actual columns, so that they are always prefixed, or just listing all the result fields with AS aliases, as Scibuff and Alister suggested.

I don't believe it can be done for all columns automatically, but you can list as many columns as you like with AS
SELECT id AS a_id,
name AS a_name,
email AS a_email /*, etc....*/
FROM tableA;
I've only inserted newlines for some extra clarity.

Related

Transpose key -> value table (rows) into columns

We have a metadata pair key value table where we store daily kpis on a similar format to:
+------+---------+-------------+------------+
| id | key | value | last_update|
+------+---------+-------------+------------+
| 1 | key1 | foo | 2022-08-08 |
| 2 | key2 | bar | 2022-08-08 |
| 3 | key4 | more | 2022-08-08 |
| 4 | key2 | galaxy | 2022-08-07 |
| 5 | key3 | foo | 2022-08-06 |
| 6 | key4 | other | 2022-08-06 |
+------+---------+-------------+------------+
Only changed data from previous value gets saved so not all keys get created every day. New keys can appear any time.
In order to represent some output charts, I am looking for query for MySQL that could transpose the values to a more "traditional" format of one per day for all the keys that exist that day.
+---------+----------+---------+--------+------------+
| key1 | key2 | key3 | key4 | date |
+---------+----------+---------+--------+------------+
| foo | bar | NULL | more | 2022-08-08 |
| NULL | galaxy | NULL | NULL | 2022-08-07 |
| NULL | NULL | foo | other | 2022-08-06 |
+---------+----------+---------+--------+------------+
I've tried different approaches to insert records based on a
select DISTINCT id from source table ORDER BY id
that was based on similar replies here in stackoverflow, but without success.
CLARIFICATION EDIT
Solutions using the keys themselces for the query are not valid as they are not fixed, and can change or be added. I want to know if any a key_reference table could be used with a row per each key used.
This is a pivot table given your example:
SELECT
MAX(CASE `key` WHEN 'key1' THEN `value` END) AS `key1`,
MAX(CASE `key` WHEN 'key2' THEN `value` END) AS `key2`,
MAX(CASE `key` WHEN 'key3' THEN `value` END) AS `key3`,
MAX(CASE `key` WHEN 'key4' THEN `value` END) AS `key4`,
last_update AS date
FROM `source table`
GROUP BY date;
You do need to know the distinct values for key to format the query.
One must assume there's only one value for a given key on a given date.
I use GROUP_CONCAT to solve this kind of problem unles you want only the first or last value of the group. Showing all associated values is important to not missing any information.
SELECT
GROUP_CONCAT(CASE `key` WHEN 'key1' THEN `value` END) AS `key1`,
GROUP_CONCAT(CASE `key` WHEN 'key2' THEN `value` END) AS `key2`,
GROUP_CONCAT(CASE `key` WHEN 'key3' THEN `value` END) AS `key3`,
GROUP_CONCAT(CASE `key` WHEN 'key4' THEN `value` END) AS `key4`,
last_update AS date
FROM `source table`
GROUP BY date;
You can also use PREPARED STATEMENT to generate the Query. In this case you can change the count of keys in your table without changing the query.
SELECT GROUP_CONCAT(mycol SEPARATOR ',\n') into #cols FROM (
SELECT CONCAT( "GROUP_CONCAT(CASE `key` WHEN '", `key`, "' THEN `value` END) AS '", `key` ,"'" ) AS mycol
FROM keytab
GROUP BY `key`
ORDER BY `key` ) as mycols;
SET #sql = CONCAT("SELECT ",#cols, ", last_update AS 'date' FROM `keytab` GROUP BY `date` ORDER BY `date` DESC");
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SAMPLE
the table
mysql> SELECT * from keytab;
+----+------+--------+-------------+
| id | key | value | last_update |
+----+------+--------+-------------+
| 1 | key1 | foo | 2022-08-08 |
| 2 | key2 | bar | 2022-08-08 |
| 3 | key4 | more | 2022-08-08 |
| 4 | key2 | galaxy | 2022-08-07 |
| 5 | key3 | foo | 2022-08-06 |
| 6 | key4 | other | 2022-08-06 |
+----+------+--------+-------------+
6 rows in set (0.00 sec)
the query
mysql> SET #sql = CONCAT("SELECT ",#cols, ", last_update AS 'date' FROM `keytab` GROUP BY `date` ORDER BY `date` DESC");
Query OK, 0 rows affected (0.01 sec)
mysql> PREPARE stmt FROM #sql;
Query OK, 0 rows affected (0.01 sec)
Statement prepared
mysql> EXECUTE stmt;
+------+--------+------+-------+------------+
| key1 | key2 | key3 | key4 | date |
+------+--------+------+-------+------------+
| foo | bar | NULL | more | 2022-08-08 |
| NULL | galaxy | NULL | NULL | 2022-08-07 |
| NULL | NULL | foo | other | 2022-08-06 |
+------+--------+------+-------+------------+
3 rows in set (0.01 sec)
mysql> DEALLOCATE PREPARE stmt;
Query OK, 0 rows affected (0.00 sec)
mysql>
add a key an try again
mysql> INSERT INTO `keytab` (`id`, `key`, `value`, `last_update`)
-> VALUES
-> (7, 'key7', 'Hello', '2022-08-05'),
-> (8, 'key9', 'Bernd', '2022-08-05');
Query OK, 2 rows affected (0.10 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> SELECT GROUP_CONCAT(mycol SEPARATOR ',\n') into #cols FROM (
-> SELECT CONCAT( "GROUP_CONCAT(CASE `key` WHEN '", `key`, "' THEN `value` END) AS '", `key` ,"'" ) AS mycol
-> FROM keytab
-> GROUP BY `key`
-> ORDER BY `key` ) as mycols;
Query OK, 1 row affected (0.04 sec)
mysql>
mysql> SET #sql = CONCAT("SELECT ",#cols, ", last_update AS 'date' FROM `keytab` GROUP BY `date` ORDER BY `date` DESC");
Query OK, 0 rows affected (0.01 sec)
mysql> PREPARE stmt FROM #sql;
Query OK, 0 rows affected (0.01 sec)
Statement prepared
mysql> EXECUTE stmt;
+------+--------+------+-------+-------+-------+------------+
| key1 | key2 | key3 | key4 | key7 | key9 | date |
+------+--------+------+-------+-------+-------+------------+
| foo | bar | NULL | more | NULL | NULL | 2022-08-08 |
| NULL | galaxy | NULL | NULL | NULL | NULL | 2022-08-07 |
| NULL | NULL | foo | other | NULL | NULL | 2022-08-06 |
| NULL | NULL | NULL | NULL | Hello | Bernd | 2022-08-05 |
+------+--------+------+-------+-------+-------+------------+
4 rows in set (0.00 sec)
mysql> DEALLOCATE PREPARE stmt;
Query OK, 0 rows affected (0.00 sec)
mysql>

How to delete columns before a certain column in SQL [duplicate]

I am trying to remove a column from a MySQL table that I am using with Handsontable. When I remove a column from the Handsontable, I can get the column's index using the afterRemoveCol() callback:
afterRemoveCol: function (index, amount) {
alert(index +' amount: '+amount);
}
I would like to remove the column using the column number (n) returned by this callback function from the MySQL table using something like:
ALTER TABLE tbl_Blah DROP COLUMN n;
So, if I want to drop column #3 from the MySQL table, How would I do this using just the column number?
To add to RMathis answer, you can do everything within SQL by also using SET to define the DROP string in conjuntion with PREPARE and EXECUTE
SQL Fiddle
MySQL 5.6 Schema Setup:
CREATE TABLE Table1
(`col1` varchar(1),
`col2` varchar(1),
`col3` varchar(1),
`col4` varchar(1),
`col5` varchar(1))
;
set #col = (select column_name
from information_schema.columns
where table_name='table1' and ordinal_position=3);
SET #s = CONCAT("alter table table1 drop column ", #col);
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Query 1:
desc table1
Results:
| COLUMN_NAME | COLUMN_TYPE | IS_NULLABLE | COLUMN_KEY | COLUMN_DEFAULT | EXTRA |
|-------------|-------------|-------------|------------|----------------|-------|
| col1 | varchar(1) | YES | | (null) | |
| col2 | varchar(1) | YES | | (null) | |
| col4 | varchar(1) | YES | | (null) | |
| col5 | varchar(1) | YES | | (null) | |
SQL from SQL can generate the drop statement using ordinal_position from information_schema.columns.
mysql test> create table test.tab (col1 char(1), col2 char(1), col3 char(1), col4 char(1), col5 char(1));
Query OK, 0 rows affected (0.01 sec)
mysql test> desc test.tab;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| col1 | char(1) | YES | | NULL | |
| col2 | char(1) | YES | | NULL | |
| col3 | char(1) | YES | | NULL | |
| col4 | char(1) | YES | | NULL | |
| col5 | char(1) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
5 rows in set (0.00 sec)
This query is generating the statement to drop the third column...
mysql test> select concat('alter table ',table_schema,'.',table_name,' drop column ',column_name,';') as query1 from information_schema.columns where table_schema='test' and table_name='tab' and ordinal_position=3;
+----------------------------------------+
| query1 |
+----------------------------------------+
| alter table test.tab drop column col3; |
+----------------------------------------+
1 row in set (0.00 sec)
Now to run the projected command...
mysql test> alter table test.tab drop column col3;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
The column definition has been removed from the table.
mysql test> desc test.tab;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| col1 | char(1) | YES | | NULL | |
| col2 | char(1) | YES | | NULL | |
| col4 | char(1) | YES | | NULL | |
| col5 | char(1) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
4 rows in set (0.01 sec)
Name them (columns in your db mirroring the spreadsheet) with the numbers/letters in full correspondence with your spreadsheet.
And then you'd be able to use regular ALTER TABLE ... DROP COLUMN.

How to DROP a column from a table in MySQL using a column number

I am trying to remove a column from a MySQL table that I am using with Handsontable. When I remove a column from the Handsontable, I can get the column's index using the afterRemoveCol() callback:
afterRemoveCol: function (index, amount) {
alert(index +' amount: '+amount);
}
I would like to remove the column using the column number (n) returned by this callback function from the MySQL table using something like:
ALTER TABLE tbl_Blah DROP COLUMN n;
So, if I want to drop column #3 from the MySQL table, How would I do this using just the column number?
To add to RMathis answer, you can do everything within SQL by also using SET to define the DROP string in conjuntion with PREPARE and EXECUTE
SQL Fiddle
MySQL 5.6 Schema Setup:
CREATE TABLE Table1
(`col1` varchar(1),
`col2` varchar(1),
`col3` varchar(1),
`col4` varchar(1),
`col5` varchar(1))
;
set #col = (select column_name
from information_schema.columns
where table_name='table1' and ordinal_position=3);
SET #s = CONCAT("alter table table1 drop column ", #col);
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Query 1:
desc table1
Results:
| COLUMN_NAME | COLUMN_TYPE | IS_NULLABLE | COLUMN_KEY | COLUMN_DEFAULT | EXTRA |
|-------------|-------------|-------------|------------|----------------|-------|
| col1 | varchar(1) | YES | | (null) | |
| col2 | varchar(1) | YES | | (null) | |
| col4 | varchar(1) | YES | | (null) | |
| col5 | varchar(1) | YES | | (null) | |
SQL from SQL can generate the drop statement using ordinal_position from information_schema.columns.
mysql test> create table test.tab (col1 char(1), col2 char(1), col3 char(1), col4 char(1), col5 char(1));
Query OK, 0 rows affected (0.01 sec)
mysql test> desc test.tab;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| col1 | char(1) | YES | | NULL | |
| col2 | char(1) | YES | | NULL | |
| col3 | char(1) | YES | | NULL | |
| col4 | char(1) | YES | | NULL | |
| col5 | char(1) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
5 rows in set (0.00 sec)
This query is generating the statement to drop the third column...
mysql test> select concat('alter table ',table_schema,'.',table_name,' drop column ',column_name,';') as query1 from information_schema.columns where table_schema='test' and table_name='tab' and ordinal_position=3;
+----------------------------------------+
| query1 |
+----------------------------------------+
| alter table test.tab drop column col3; |
+----------------------------------------+
1 row in set (0.00 sec)
Now to run the projected command...
mysql test> alter table test.tab drop column col3;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
The column definition has been removed from the table.
mysql test> desc test.tab;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| col1 | char(1) | YES | | NULL | |
| col2 | char(1) | YES | | NULL | |
| col4 | char(1) | YES | | NULL | |
| col5 | char(1) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
4 rows in set (0.01 sec)
Name them (columns in your db mirroring the spreadsheet) with the numbers/letters in full correspondence with your spreadsheet.
And then you'd be able to use regular ALTER TABLE ... DROP COLUMN.

MySQL Insert from 2 source tables to one destination table

I am having issues inserting Id fields from two tables into a single record in a third table.
mysql> describe ing_titles;
+----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+----------------+
| ID_Title | int(10) unsigned | NO | PRI | NULL | auto_increment |
| title | varchar(64) | NO | UNI | NULL | |
+----------+------------------+------+-----+---------+----------------+
2 rows in set (0.22 sec)
mysql> describe ing_categories;
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| ID_Category | int(10) unsigned | NO | PRI | NULL | auto_increment |
| category | varchar(64) | NO | UNI | NULL | |
+-------------+------------------+------+-----+---------+----------------+
2 rows in set (0.02 sec)
mysql> describe ing_title_categories;
+-------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+------------------+------+-----+---------+----------------+
| ID_Title_Category | int(10) unsigned | NO | PRI | NULL | auto_increment |
| ID_Title | int(10) unsigned | NO | MUL | NULL | |
| ID_Category | int(10) unsigned | NO | MUL | NULL | |
+-------------------+------------------+------+-----+---------+----------------+
3 rows in set (0.04 sec)
Let's say the data from the tables is:
mysql> select * from ing_titles;
+----------+-------------------+
| ID_Title | title |
+----------+-------------------+
| 3 | Chicken |
| 2 | corn |
| 1 | Fettucini Alfredo |
+----------+-------------------+
3 rows in set (0.00 sec)
mysql> select * from ing_categories;
+-------------+----------+
| ID_Category | category |
+-------------+----------+
| 1 | Dinner |
| 3 | Meat |
| 2 | Veggie |
+-------------+----------+
3 rows in set (0.00 sec)
I want to insert into ing_title_categories the record "corn, Veggie" or where ID_Title = 2 and ID_Category = 2.
Here's what I tried:
INSERT INTO ing_title_categories (ID_Title, ID_Category)
SELECT ing_titles.ID_Title, ing_categories.ID_Category
FROM ing_title_categories
LEFT JOIN ing_titles ON ing_title_categories.ID_Title=ing_titles.ID_Title
LEFT JOIN ing_categories ON ing_title_categories.ID_Category=ing_categories.ID_Category
WHERE (ing_titles.ID_Title=2) AND (ing_categories.ID_Category = 2);
There is no data inserted into the table ing_title_categories, and here is the reply from MySQL:
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
What is the correct syntax for inserting the ing_titles.ID_Title and ing_categories.ID_Category into the table ing_titles_categories?
Please, no PHP or Python examples. Use SQL that I can copy and paste into the MySQL prompt. I will be adding this to a C++ program, not PHP, JavaScript or Python.
Edit 1:
The ing_title_categories.ID_Title and ing_title_categories.ID_Category are foreign keys into the other tables.
INSERT INTO
ing_title_categories (ID_Title, ID_Category)
SELECT
ing_titles.ID_Title, ing_categories.ID_Category
FROM
ing_titles, ing_categories
WHERE
ing_titles.ID_Title = ing_categories.ID_Category AND
ing_titles.ID_Title = 2 AND ing_categories.ID_Category = 2;
SQL Fiddle demo
After taking advice from #DrewPierce and #KaiserM11, here is the MySQL sequence:
mysql> INSERT INTO ing_title_categories (ID_Title, ID_Category)
-> SELECT
-> ing_titles.ID_Title,
-> ing_categories.ID_Category
-> FROM ing_titles, ing_categories
-> where (ing_titles.ID_Title = 2) AND (ing_categories.ID_Category = 2)
-> ;
Query OK, 1 row affected (0.07 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from ing_title_categories;
+-------------------+----------+-------------+
| ID_Title_Category | ID_Title | ID_Category |
+-------------------+----------+-------------+
| 17 | 2 | 2 |
+-------------------+----------+-------------+
1 row in set (0.00 sec)
In this case, only possible way I see is using a UNION query like
INSERT INTO ing_title_categories (ID_Title, ID_Category)
SELECT Title, NULL
FROM ing_title WHERE ID_Title = 2
UNION
SELECT NULL, category
FROM ing_categories
WHERE ID_Category = 2
(OR)
You can change your table design and use an AFTER INSERT trigger to perform the same in one go.
EDIT:
If you can change your table design to something like below (No need of that extra chaining table)
ing_titles(ID_Title int not null auto_increment PK, title varchar(64) not null);
ing_categories( ID_Category int not null auto_increment PK,
category varchar(64) not null,
ing_titles_ID_Title int not null,
FOREIGN KEY (ing_titles_ID_Title)
REFERENCES ing_titles(ID_Title));
Then you can use a AFTER INSERT trigger and do the insertion like
DELIMITER //
CREATE TRIGGER ing_titles_after_insert
AFTER INSERT
ON ing_titles FOR EACH ROW
BEGIN
-- Insert record into ing_categories table
INSERT INTO ing_categories
( category,
ing_titles_ID_Title)
VALUES
('Meat' NEW.ID_Title);
END; //
DELIMITER ;

MySQL: Strange AUTO_INCREMENT

Table looks like:
mysql> DESC text;
+-----------------+--------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| text | varchar(255) | YES | | NULL | |
+-----------------+--------------+------+-----+-------------------+----------------+
2 rows in set (0.00 sec)
and AUTO_INCREMENT is 1:
mysql> ALTER TABLE text AUTO_INCREMENT = 1;
Query OK, 1 row affected (0.36 sec)
Records: 1 Duplicates: 0 Warnings: 0
but I get strange id like:
mysql> SELECT id FROM text;
+------------+
| id |
+------------+
| 2147483647 |
+------------+
1 row in set (0.00 sec)
What is the problem?
When you change the auto increment it is set to greatest(your_value,max(column)+ 1)
though I cant find the part in the docs which mention it, it is in the comments
http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
ALTER TABLE text AUTO_INCREMENT = 1;
then check the result of
SELECT `AUTO_INCREMENT`
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'DatabaseName'
AND TABLE_NAME = 'text';
to confirm that its not actually 1