Pass a value from a Table to another Table - mysql

I have this table, its name is puntajes:
+---------------+---------------+---------+
| estudiante_ID | evaluacion_ID | puntaje |
+---------------+---------------+---------+
| 1 | 1 | 15 |
| 2 | 1 | 11 |
| 3 | 1 | 17 |
| 4 | 1 | 12 |
| 1 | 2 | 13 |
| 2 | 2 | 8 |
| 3 | 2 | 15 |
| 4 | 2 | 16 |
| 1 | 3 | 9 |
| 2 | 3 | 14 |
| 3 | 3 | 9 |
| 4 | 3 | 10 |
| 1 | 4 | 15 |
| 2 | 4 | 16 |
| 3 | 4 | 9 |
| 4 | 4 | 12 |
+---------------+---------------+---------+
And I want to get the max score from puntaje column where evaluacion_ID is equal to 3, i want this value to be in the 'maxpuntaje 'column in the evaluaciones table, like an update , this is evaluaciones table:
+---------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+----------------+
| fecha | date | NO | | NULL | |
| tipo | enum('P','E') | NO | | NULL | |
| maxpuntaje | int(11) | NO | | NULL | |
| clase_ID | int(10) unsigned | NO | | NULL | |
| evaluacion_ID | int(10) unsigned | NO | PRI | NULL | auto_increment |
+---------------+------------------+------+-----+---------+----------------+
both of the tables have the evaluacion_ID column

I've commented out WHERE conditions that limit the UPDATE and maximum puntaje value retrieval so that your query could be run for all evaluacion_ID. If you need it only for 3, remove the comment marks.
Using MySQL UPDATE JOIN syntax:
UPDATE evaluaciones e
LEFT JOIN (
SELECT evaluacion_ID, MAX(puntaje) maxpuntaje
FROM puntajes
-- WHERE evaluacion_ID = 3
GROUP BY evaluacion_ID
) p USING (evaluacion_ID)
SET maxpuntaje = p.maxpuntaje
-- WHERE e.evaluacion_ID = 3

I suspect you're looking for this query:
UPDATE evaluaciones
SET maxpuntaje = (
SELECT max(puntaje)
FROM puntajes
WHERE evaluacion_ID = evaluaciones.evaluacion_ID
)
WHERE evaluacion_ID = 3

Related

Complex INSERT or UPDATE MariaDB tables with data from other MariaDB tables using JOIN or UNION

I need to INSERT or UPDATE data in a table using data from other tables; I understand the basic
insert into table (a,b,c)
select h, i, j
from otherTable
where........
My challenge comes from the fact that the data is spread across multiple tables and in one of the tables the data is metadata stored in rows, not columns. Therefore I need to use JOIN and possible UNION to get what is needed.
Unfortunately after trying everything I read in both the Maria manual, on the Maria forum and on Stack overflow I can not get it to work.
Here is what I am attempting to do:
insert data into dbc_jot_groupmembers in the following fields using source data as shown:
jot_grpid = dbc_bp_groups_members.group_id
jot_bbmemid = dbc_bp_groups_members.user_id
jot_grpmemname = dbc_bp_xprofile_data.value where field_id=3
jot_grpmemnum = dbc_bp_xprofile_data.value where field_id=4
I need the final result to look like this:
select * from dbc_jot_groupmembers;
+--------------+-----------+----------------+---------------+---------------------+-------------+
| jot_grpmemid | jot_grpid | jot_grpmemname | jot_grpmemnum | jot_grpmemts | jot_bbmemid |
+--------------+-----------+----------------+---------------+---------------------+-------------+
| 1 | 17 | hutchdad | +17047047045 | 2021-06-15 14:56:19 | 14 |
| 2 | 24 | hutchdad | +17047047045 | 2021-06-15 19:49:58 | 14 |
| 3 | 25 | hutchdad | +17047047045 | 2021-06-15 19:49:58 | 14 |
| 4 | 17 | hutchmom | +17773274355 | 2021-06-15 19:49:58 | 15 |
| 5 | 24 | hutchmom | +17773274355 | 2021-06-15 19:49:58 | 15 |
| 6 | 16 | ledwards | +14567655645 | 2021-06-15 19:49:58 | 11 |
| 7 | 16 | medwards | +12223334545 | 2021-06-15 19:49:58 | 10 |
| 7 | 20 | medwards | +12223334545 | 2021-06-15 19:49:58 | 10 |
SAMPLE DATA FROM SOURCE TABELS AND TABLE DEFINITIONS:
MariaDB [devDisciplePlaceCom]> describe dbc_bp_groups_members;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| group_id | bigint(20) | NO | MUL | NULL | |
| user_id | bigint(20) | NO | MUL | NULL | |
| inviter_id | bigint(20) | NO | MUL | NULL | |
| is_admin | tinyint(1) | NO | MUL | 0 | |
| is_mod | tinyint(1) | NO | MUL | 0 | |
| user_title | varchar(100) | NO | | NULL | |
| date_modified | datetime | NO | | NULL | |
| comments | longtext | NO | | NULL | |
| is_confirmed | tinyint(1) | NO | MUL | 0 | |
| is_banned | tinyint(1) | NO | | 0 | |
| invite_sent | tinyint(1) | NO | | 0 | |
+---------------+--------------+------+-----+---------+----------------+
12 rows in set (0.002 sec)
describe dbc_bp_xprofile_data;
+--------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| field_id | bigint(20) unsigned | NO | MUL | NULL | |
| user_id | bigint(20) unsigned | NO | MUL | NULL | |
| value | longtext | NO | | NULL | |
| last_updated | datetime | NO | | NULL | |
+--------------+---------------------+------+-----+---------+----------------+
5 rows in set (0.001 sec)
THIS IS THE LIST OF GROUPS AND WHAT USERS THEY ARE IN.
select group_id,user_id from dbc_bp_groups_members ;
+----------+---------+
| group_id | user_id |
+----------+---------+
| 16 | 13 |
| 16 | 12 |
| 16 | 11 |
| 16 | 10 |
| 17 | 14 |
| 17 | 15 |
| 17 | 16 |
| 17 | 17 |
| 17 | 18 |
| 17 | 19 |
| 20 | 10 |
| 24 | 14 |
| 24 | 16 |
| 24 | 15 |
| 24 | 17 |
| 24 | 19 |
| 25 | 19 |
| 25 | 14 |
| 1 | 14 |
| 11 | 14 |
+----------+---------+
20 rows in set (0.000 sec)
THIS IS THE TABLE CONTAINING THE USERS METADATA. IN MY CASE I NEED THE PHOEN NUMBER AND NAME WHICH ARE IN THE value FIELD WITH A field_id of 3 and 4.
select * from dbc_bp_xprofile_data where user_id > 9 and field_id > 2 AND field_id < 5;
+-----+----------+---------+---------------+---------------------+
| id | field_id | user_id | value | last_updated |
+-----+----------+---------+---------------+---------------------+
| 31 | 3 | 10 | medwards | 2021-06-24 03:11:59 |
| 34 | 3 | 11 | ledwards | 2021-06-24 03:11:24 |
| 37 | 3 | 12 | nedwards | 2021-04-24 14:47:18 |
| 40 | 3 | 13 | iedwards | 2021-04-24 14:47:52 |
| 43 | 3 | 14 | hutchdad | 2021-06-21 14:53:08 |
| 46 | 3 | 15 | hutchmom | 2021-06-24 03:10:58 |
| 49 | 3 | 16 | hutchdaughter | 2021-04-24 16:54:48 |
| 52 | 3 | 17 | hutchson1 | 2021-04-24 16:55:43 |
| 55 | 3 | 18 | hutchson2 | 2021-04-24 16:57:42 |
| 58 | 3 | 19 | hutchson3 | 2021-04-24 16:58:44 |
| 78 | 3 | 25 | demoadmin | 2021-06-08 02:01:39 |
| 158 | 4 | 14 | 7047047045 | 2021-06-21 14:53:08 |
| 190 | 3 | 58 | dupdup | 2021-06-23 19:46:19 |
| 191 | 4 | 15 | 7773274355 | 2021-06-24 03:10:58 |
| 193 | 4 | 11 | 4567655645 | 2021-06-24 03:11:24 |
| 195 | 4 | 10 | 2223334545 | 2021-06-24 03:11:59 |
+-----+----------+---------+---------------+---------------------+
16 rows in set (0.000 sec)
If this can not be done is a single INSERT then I can use an INSERT with subsequent UPDATE statements. I also understand that this is not best practice and violates 3nf and probably several other best practice principles. Unfortunately, I am at the mercy of the application and can not change the code, so the only way to get this to work is to put duplicate data in the database as described below:
It can be done with a single INSERT. However, there are some information need to be addressed as what I've posted in a the comment. In the meantime, here is an example query that you can use to do the operation that you want:
SELECT ROW_NUMBER() OVER (ORDER BY A.group_id, A.user_id) AS 'jot_grpmemid',
A.group_id AS 'jot_grpid',
MAX(CASE WHEN B.field_id=3 THEN B.value ELSE '' END) AS 'jot_grpmemname',
MAX(CASE WHEN B.field_id=4 THEN CONCAT('+',B.value) ELSE '' END) AS 'jot_grpmemnum',
A.user_id AS 'jot_bbmemid'
FROM
dbc_bp_groups_members A JOIN dbc_bp_xprofile_data B
ON A.user_id=B.user_id
GROUP BY A.group_id, A.user_id;
Like I said in the comment, I'm not sure how you get/generate jot_grpmemid because you have two 7 in the expected result so I assume it's a typo. I guess, at this point it's up to you to modify the query accordingly.
Demo fiddle

Multiple Selects on Insert Statement - mySQL

I have 2 tables.
1.
**code**
id
name
2.
**code_category**
id
code_id
category_id
discount
I have a list of 15000 codes in the database without the code_category yet. I only have 1 sample one to reference in code_category. I have to mass add in the code_category with 20 categories per code.
I am thinking of left joining both tables to get where code_id is null that gets me all the un-attached codes but I need another select statement to get the 20 different types of code_category rows to enter per code.
insert into code_category (code_id, category_id, discount)
values
(select id from code c
left join code_category cc on cc.code_id = c.id
where cc.code_id is null)
union
(select categoryid, discount from code_category where c.id = 123)
123 being the reference id.
I built a simplified example of your tables in a SQL Fiddle, then ran just the select (shown below) to see what rows would be inserted. See if this example answers your question.
Note how in the select I left join twice, once to get a list of categories, and again to find the missing ones.
SQL Fiddle
MySQL 5.6 Schema Setup:
CREATE TABLE IF NOT EXISTS `code` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
ENGINE=MyISAM
AUTO_INCREMENT=1
DEFAULT CHARSET=utf8
COLLATE=utf8_unicode_ci
COMMENT '';
CREATE TABLE IF NOT EXISTS `code_category` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`codeid` INT(11) UNSIGNED NULL DEFAULT NULL,
`categoryid` INT(11) UNSIGNED NULL DEFAULT NULL,
`discount` INT(11) UNSIGNED NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_codecategory_catid` (`categoryid`),
KEY `idx_codecategory_codeid` (`codeid`)
)
ENGINE=MyISAM
AUTO_INCREMENT=1
DEFAULT CHARSET=utf8
COLLATE=utf8_unicode_ci
COMMENT '';
INSERT INTO `code`
(`id`,`name`)
VALUES
(1,'code1'),
(2,'code2'),
(3,'code3'),
(4,'code4');
INSERT INTO `code_category`
(`codeid`,`categoryid`,`discount`)
VALUES
(1,1,3),
(2,1,2),
(1,2,2),
(1,3,3),
(1,4,2),
(1,5,2),
(1,6,2),
(1,7,2),
(1,8,2),
(1,9,2),
(1,10,2),
(1,11,2),
(1,12,2),
(1,13,2),
(1,14,2),
(1,15,2),
(1,16,2),
(1,17,2),
(1,18,2),
(1,19,2),
(1,20,2);
Query 1:
SELECT
c.id,
cc1.codeid,
cc1.categoryid,
cc1.discount
FROM code c
LEFT JOIN code_category cc1
ON cc1.codeid = 1
LEFT JOIN code_category cc
ON cc.codeid = c.id AND cc.codeid = cc1.codeid
WHERE cc.codeid is null
ORDER BY c.id,cc1.codeid,cc1.categoryid
Results:
| id | codeid | categoryid | discount |
|----|--------|------------|----------|
| 2 | 1 | 1 | 3 |
| 2 | 1 | 2 | 2 |
| 2 | 1 | 3 | 3 |
| 2 | 1 | 4 | 2 |
| 2 | 1 | 5 | 2 |
| 2 | 1 | 6 | 2 |
| 2 | 1 | 7 | 2 |
| 2 | 1 | 8 | 2 |
| 2 | 1 | 9 | 2 |
| 2 | 1 | 10 | 2 |
| 2 | 1 | 11 | 2 |
| 2 | 1 | 12 | 2 |
| 2 | 1 | 13 | 2 |
| 2 | 1 | 14 | 2 |
| 2 | 1 | 15 | 2 |
| 2 | 1 | 16 | 2 |
| 2 | 1 | 17 | 2 |
| 2 | 1 | 18 | 2 |
| 2 | 1 | 19 | 2 |
| 2 | 1 | 20 | 2 |
| 3 | 1 | 1 | 3 |
| 3 | 1 | 2 | 2 |
| 3 | 1 | 3 | 3 |
| 3 | 1 | 4 | 2 |
| 3 | 1 | 5 | 2 |
| 3 | 1 | 6 | 2 |
| 3 | 1 | 7 | 2 |
| 3 | 1 | 8 | 2 |
| 3 | 1 | 9 | 2 |
| 3 | 1 | 10 | 2 |
| 3 | 1 | 11 | 2 |
| 3 | 1 | 12 | 2 |
| 3 | 1 | 13 | 2 |
| 3 | 1 | 14 | 2 |
| 3 | 1 | 15 | 2 |
| 3 | 1 | 16 | 2 |
| 3 | 1 | 17 | 2 |
| 3 | 1 | 18 | 2 |
| 3 | 1 | 19 | 2 |
| 3 | 1 | 20 | 2 |
| 4 | 1 | 1 | 3 |
| 4 | 1 | 2 | 2 |
| 4 | 1 | 3 | 3 |
| 4 | 1 | 4 | 2 |
| 4 | 1 | 5 | 2 |
| 4 | 1 | 6 | 2 |
| 4 | 1 | 7 | 2 |
| 4 | 1 | 8 | 2 |
| 4 | 1 | 9 | 2 |
| 4 | 1 | 10 | 2 |
| 4 | 1 | 11 | 2 |
| 4 | 1 | 12 | 2 |
| 4 | 1 | 13 | 2 |
| 4 | 1 | 14 | 2 |
| 4 | 1 | 15 | 2 |
| 4 | 1 | 16 | 2 |
| 4 | 1 | 17 | 2 |
| 4 | 1 | 18 | 2 |
| 4 | 1 | 19 | 2 |
| 4 | 1 | 20 | 2 |
|----|--------|------------|----------|
You may need to change the column names to match your tables.

How can I merge multiple rows with same ID into one row?

How can I merge multiple rows with same ID into one row.
I have table:
+----+------+------+------+
| ID | A | B | C |
+----+------+------+------+
| 1 | 123 | 31 | 456 |
| 1 | 412 | NULL | 1 |
| 2 | 567 | 38 | 4 |
| 2 | 567 | NULL | NULL |
| 3 | 2 | NULL | NULL |
| 3 | 5 | NULL | NULL |
| 4 | 6 | 1 | NULL |
| 4 | 8 | NULL | 5 |
| 4 | NULL | NULL | 5 |
+----+------+------+------+
I want to have table :
I have table:
+----+-----+------+------+-----+------+------+------+------+----+
| ID | A | B | C | A2 | B2 | C2 | A3 | B3 | C3 |
+----+-----+------+------+-----+------+------+------+------+----+
| 1 | 123 | 31 | 456 | 412 | NULL | 1 | | | |
| 2 | 567 | 38 | 4 | 567 | NULL | NULL | | | |
| 3 | 2 | NULL | NULL | 5 | NULL | NULL | | | |
| 4 | 6 | 1 | NULL | 8 | NULL | 5 | NULL | NULL | 5 |
+----+-----+------+------+-----+------+------+------+------+----+
Try This:-
1 :- Please Change data table structure to disallow null value.
Query :-
SELECT
GROUP_CONCAT(if (`A` ='0', 'NOVAL', `A`)SEPARATOR '----') as A,
GROUP_CONCAT(if (`B` ='0', 'NOVAL', `B`)SEPARATOR '----') as B,
GROUP_CONCAT(if (`C` ='0', 'NOVAL', `C`)SEPARATOR '----') as C
FROM que1
GROUP BY id
Now You can get the value using mysql_fetch_array

Select Same Column From 2 Tables into 1 Column in View

I have two column with the same name in different tables.
I want to join them into one column in a view.
Here my first table stocks:
+----------+------------+------------+---------+----------------+--------+
| stock_id | stock_cost | stock_left | item_id | purchasedtl_id | trx_id |
+----------+------------+------------+---------+----------------+--------+
| 1 | 1000 | 0 | 1 | 1 | 1 |
| 2 | 1000 | 5 | 1 | 2 | 2 |
| 3 | 1000 | 1 | 1 | 3 | 4 |
+----------+------------+------------+---------+----------------+--------+
Second table stocks_out
+-------------+----------------+--------------+---------+----------+------------+--------+
| stockout_id | stockout_price | stockout_qty | item_id | stock_id | saledtl_id | trx_id |
+-------------+----------------+--------------+---------+----------+------------+--------+
| 1 | 2000 | 1 | 1 | 1 | 1 | 3 |
+-------------+----------------+--------------+---------+----------+------------+--------+
And I want to join them to be like this trx_id, trx_no, trx_closetime, trx_type stock_id, stock_cost, stockout_id, stock_out_cost, stockout_price, item_id
item_id is the field I want to join in one column.
The current Query is :
select `transactions`.`trx_id` AS `trx_id`,`transactions`.`trx_no` AS `trx_no`,`transactions`.`trx_closetime` AS `trx_closetime`,`transactions`.`trx_type` AS `trx_type`,`stocks`.`stock_id` AS `stock_id`,`stocks`.`stock_cost` AS `stock_cost`,`stock_out`.`stockout_id` AS `stockout_id`,`stock_out`.`stockout_price` AS `stockout_price` from ((`transactions` left join `stocks` on(`stocks`.`trx_id` = `transactions`.`trx_id`)) left join `stock_out` on(`stock_out`.`trx_id` = `transactions`.`trx_id`)) order by `transactions`.`trx_closetime`;
And the current result:
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+
| trx_id | trx_no | trx_closetime | trx_type | stock_id | stock_cost | stockout_id | stockout_price |
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+
| 1 | 02002-02-170415-001 | 2017-04-15 19:40:03 | 2 | 1 | 1000 | NULL | NULL |
| 2 | 02002-02-170415-002 | 2017-04-15 19:40:13 | 2 | 2 | 1000 | NULL | NULL |
| 3 | 02002-01-170415-001 | 2017-04-15 19:40:57 | 1 | NULL | NULL | 1 | 2000 |
| 4 | 02002-02-170415-003 | 2017-04-15 19:41:14 | 2 | 3 | 1000 | NULL | NULL |
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+
Found it guys.
I just need to add the following query as the column
COALESCE(`stocks`.`item_id`, `stocks_out`.`item_id`) AS `item_id`
So the query will be like
select `transactions`.`trx_id` AS `trx_id`,`transactions`.`trx_no` AS `trx_no`,`transactions`.`trx_closetime` AS `trx_closetime`,`transactions`.`trx_type` AS `trx_type`,`stocks`.`stock_id` AS `stock_id`,`stocks`.`stock_cost` AS `stock_cost`,`stock_out`.`stockout_id` AS `stockout_id`,`stock_out`.`stockout_price` AS `stockout_price`, COALESCE(`stocks`.`item_id`, `stocks_out`.`item_id`) AS `item_id from ((`transactions` left join `stocks` on(`stocks`.`trx_id` = `transactions`.`trx_id`)) left join `stock_out` on(`stock_out`.`trx_id` = `transactions`.`trx_id`)) order by `transactions`.`trx_closetime`;
And the result:
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+---------+
| trx_id | trx_no | trx_closetime | trx_type | stock_id | stock_cost | stockout_id | stockout_price | item_id |
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+---------+
| 1 | 02002-02-170415-001 | 2017-04-15 19:40:03 | 2 | 1 | 1000 | NULL | NULL | 1 |
| 2 | 02002-02-170415-002 | 2017-04-15 19:40:13 | 2 | 2 | 1000 | NULL | NULL | 1 |
| 3 | 02002-01-170415-001 | 2017-04-15 19:40:57 | 1 | NULL | NULL | 1 | 2000 | 1 |
| 4 | 02002-02-170415-003 | 2017-04-15 19:41:14 | 2 | 3 | 1000 | NULL | NULL | 1 |
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+---------+

How to join 3 tables in order to get a result with two tables alternating

Say I have 3 tables, foo bar and baz, where bar and baz having different additional informations about the datasets in foo. Is there a way to join these 3 tables together, so that in every output row is either a (foo <join> bar) or (foo <join> baz) dataset?
Say, I have the following tables:
mysql> select * from foo;
+----+-------+
| id | foo |
+----+-------+
| 1 | start |
| 2 | mid |
| 3 | end |
+----+-------+
mysql> select * from bar;
+----+-----+-----------+
| id | bid | bar |
+----+-----+-----------+
| 1 | 1 | bar-start |
| 2 | 2 | bar-mid |
| 3 | 3 | bar-end |
+----+-----+-----------+
mysql> select * from baz;
+----+-----+-------------+
| id | bid | baz |
+----+-----+-------------+
| 1 | 1 | baz-start-1 |
| 1 | 2 | baz-start-2 |
| 1 | 3 | baz-start-3 |
| 2 | 4 | baz-mid-1 |
| 2 | 5 | baz-mid-2 |
| 2 | 6 | baz-mid-3 |
| 3 | 7 | baz-end-1 |
| 3 | 8 | baz-end-2 |
| 3 | 9 | baz-end-3 |
+----+-----+-------------+
I can extract all the information with a query which joins those tables together via
mysql> select * from foo join bar join baz on foo.id = bar.id and foo.id=baz.id;
+----+-------+----+-----+-----------+----+-----+-------------+
| id | foo | id | bid | bar | id | bid | baz |
+----+-------+----+-----+-----------+----+-----+-------------+
| 1 | start | 1 | 1 | bar-start | 1 | 1 | baz-start-1 |
| 1 | start | 1 | 1 | bar-start | 1 | 2 | baz-start-2 |
| 1 | start | 1 | 1 | bar-start | 1 | 3 | baz-start-3 |
| 2 | mid | 2 | 2 | bar-mid | 2 | 4 | baz-mid-1 |
| 2 | mid | 2 | 2 | bar-mid | 2 | 5 | baz-mid-2 |
| 2 | mid | 2 | 2 | bar-mid | 2 | 6 | baz-mid-3 |
| 3 | end | 3 | 3 | bar-end | 3 | 7 | baz-end-1 |
| 3 | end | 3 | 3 | bar-end | 3 | 8 | baz-end-2 |
| 3 | end | 3 | 3 | bar-end | 3 | 9 | baz-end-3 |
+----+-------+----+-----+-----------+----+-----+-------------+
But I would like to get an output like the following table, since that would make the code in the application consuming the data much simpler:
+----+-------+------+------+-----------+------+------+-------------+
| id | foo | id | bid | bar | id | bid | baz |
+----+-------+------+------+-----------+------+------+-------------+
| 1 | start | 1 | 1 | bar-start | NULL | NULL | NULL |
| 1 | start | NULL | NULL | | 1 | 1 | baz-start-1 |
| 1 | start | NULL | NULL | | 1 | 2 | baz-start-2 |
| 1 | start | NULL | NULL | | 1 | 3 | baz-start-3 |
| 2 | mid | 2 | 2 | bar-mid | NULL | NULL | NULL |
| 2 | mid | NULL | NULL | | 2 | 4 | baz-mid-1 |
| 2 | mid | NULL | NULL | | 2 | 5 | baz-mid-2 |
| 2 | mid | NULL | NULL | | 2 | 6 | baz-mid-3 |
| 3 | end | 3 | 3 | bar-end | NULL | NULL | NULL |
| 3 | end | NULL | NULL | | 3 | 7 | baz-end-1 |
| 3 | end | NULL | NULL | | 3 | 8 | baz-end-2 |
| 3 | end | NULL | NULL | | 3 | 9 | baz-end-3 |
+----+-------+------+------+-----------+------+------+-------------+
Is there a way to convince mysql to give me a result set like the last one?
(
select foo.id as foo_id, foo.foo, bar.id, bar.bid, bar.bar, null, null as baz_bid, null as baz from foo
join bar on foo.id=bar.id
)
union
(
select foo.id as foo_id, foo.foo, null, null, '', baz.id, baz.bid as baz_bid, baz.baz from foo
join baz on foo.id=baz.id
)
order by foo_id asc, bar desc, baz_bid asc