I want to combine two table to get result - mysql

i have two tables,
1.Orders,
2.Items.
1.Orders Skeleton
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2.Items Skeleton
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| order_id | int(11) | NO | MUL | NULL | |
| name | varchar(50) | NO | | NULL | |
| price | int(11) | NO | | NULL | |
+----------+-------------+------+-----+---------+----------------+
Records :
1.Orders Table with Data
+----+--------+
| id | name |
+----+--------+
| 1 | Order1 |
| 2 | Order2 |
| 3 | Order3 |
+----+--------+
2.Items Table with Data
+----+----------+---------+-------+
| id | order_id | name | price |
+----+----------+---------+-------+
| 1 | 1 | Mobile1 | 25000 |
| 2 | 1 | Mobile2 | 30000 |
| 3 | 1 | Mobile3 | 6500 |
| 4 | 2 | Mobile4 | 10000 |
+----+----------+---------+-------+
I want to get items which are having same order_id,
Select id[Orders], order_id[items], name[items]1, name[items]2, name[items]3...
Suggestions Pls...

It seems that you only need to access table items to get what you need.
SELECT order_id, GROUP_CONCAT( name SEPARATOR ', ') item_names
FROM items
GROUP BY order_id;

I get solution for only known number of fields....
Row to Column:
STEP : 1
select #orderID:=order_id,
#name:=GROUP_CONCAT(name) item_names,
#price:=GROUP_CONCAT(price) item_price
from item group by order_id limit 1;
OUTPUT :
+----------+-------------------------+------------------+
| order_id | item_names | item_price |
+----------+-------------------------+------------------+
| 1 | Mobile1,Mobile2,Mobile3 | 25000,30000,6500 |
+----------+-------------------------+------------------+
STEP : 2
select #orderID,
SUBSTRING_INDEX(SUBSTRING_INDEX(#name, ',', 1),',',-1) as Name1,
SUBSTRING_INDEX(SUBSTRING_INDEX(#price, ',', 1),',',-1) as Price1,
SUBSTRING_INDEX(SUBSTRING_INDEX(#name, ',', 2),',',-1) as Name2,
SUBSTRING_INDEX(SUBSTRING_INDEX(#price, ',', 2),',',-1) as Price2,
SUBSTRING_INDEX(SUBSTRING_INDEX(#name, ',', 3),',',-1) as Name3,
SUBSTRING_INDEX(SUBSTRING_INDEX(#price, ',', 3),',',-1) as Price3;
OUTPUT :
+----------+---------+--------+---------+--------+---------+--------+
| #orderID | Name1 | Price1 | Name2 | Price2 | Name3 | Price3 |
+----------+---------+--------+---------+--------+---------+--------+
| 1 | Mobile1 | 25000 | Mobile2 | 30000 | Mobile3 | 6500 |
+----------+---------+--------+---------+--------+---------+--------+

Related

MySQL Query into single multi table join

End goal is to get tbl_output.output based on intent and slot attributes. I am currently handling it programmatically and would like to combine into one query if possible. I am open to restructuring any of the tables if needed.
SELECT id as intent_id FROM `tbl_intent` WHERE `name` = 'Car'
SELECT id as slot_id1 FROM `tbl_slot` WHERE `name` = '2018'
SELECT id as slot_id2 FROM `tbl_slot` WHERE `name` = 'Chevrolet'
SELECT id as slot_id3 FROM `tbl_slot` WHERE `name` = 'Corvette'
Example should return tbl_output ID 1 field "output".
SELECT
*,
output_id
FROM xref_intent_slot
LEFT JOIN tbl_slot slot1 ON xref_intent_slot.slot_id=slot1.id AND slot1.name='2018'
LEFT JOIN tbl_slot slot2 ON xref_intent_slot.slot_id=slot2.id AND slot2.name='Chevrolet'
LEFT JOIN tbl_slot slot3 ON xref_intent_slot.slot_id=slot3.id AND slot3.name='Corvette'
WHERE `intent_id` = (SELECT id from tbl_intent WHERE `name` = 'Car')
+----+-----------+-----------+---------+------+------+------+-----------+------+----------+-----------+
| id | output_id | intent_id | slot_id | id | name | id | name | id | name | output_id |
+----+-----------+-----------+---------+------+------+------+-----------+------+----------+-----------+
| 1 | 1 | 1 | 1 | 1 | 2018 | NULL | NULL | NULL | NULL | 1 |
| 2 | 1 | 1 | 2 | NULL | NULL | 2 | Chevrolet | NULL | NULL | 1 |
| 3 | 1 | 1 | 3 | NULL | NULL | NULL | NULL | 3 | Corvette | 1 |
| 4 | 2 | 1 | 4 | NULL | NULL | NULL | NULL | NULL | NULL | 2 |
| 5 | 2 | 1 | 2 | NULL | NULL | 2 | Chevrolet | NULL | NULL | 2 |
| 6 | 2 | 1 | 5 | NULL | NULL | NULL | NULL | NULL | NULL | 2 |
+----+-----------+-----------+---------+------+------+------+-----------+------+----------+-----------+
This should only return ID 1, 2, and 3 which is output_id 1. Then use that value to get the output from tbl_output?
Tables:
+-------------------+
| tbl_intent |
| tbl_output |
| tbl_slot |
| xref_intent_slot |
+-------------------+
Table: tbl_intent
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | MUL | NULL | |
+-------+--------------+------+-----+---------+----------------+
+----+------+
| id | name |
+----+------+
| 1 | Car |
+----+------+
Table: tbl_slot
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | MUL | NULL | |
+-------+--------------+------+-----+---------+----------------+
+----+-----------+
| id | name |
+----+-----------+
| 1 | 2018 |
| 2 | Chevrolet |
| 3 | Corvette |
| 4 | 2017 |
| 5 | Camaro |
+----+-----------+
Table: tbl_output
+--------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| output | text | NO | | NULL | |
+--------+---------+------+-----+---------+----------------+
+----+----------------+
| id | output |
+----+----------------+
| 1 | Found Corvette |
| 2 | Found Camaro |
+----+----------------+
Table: xref_intent_slot
+-----------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| output_id | int(11) | NO | | NULL | |
| intent_id | int(11) | NO | | NULL | |
| slot_id | int(11) | NO | | NULL | |
+-----------+---------+------+-----+---------+----------------+
+----+-----------+-----------+---------+
| id | output_id | intent_id | slot_id |
+----+-----------+-----------+---------+
| 1 | 1 | 1 | 1 |
| 2 | 1 | 1 | 2 |
| 3 | 1 | 1 | 3 |
| 4 | 2 | 1 | 4 |
| 5 | 2 | 1 | 2 |
| 6 | 2 | 1 | 5 |
+----+-----------+-----------+---------+
SELECT
`output`
FROM tbl_output
WHERE
id =
(
SELECT
output_id
FROM xref_intent_slot
LEFT JOIN tbl_slot slot1 ON xref_intent_slot.slot_id=slot1.id AND slot1.name='2018'
LEFT JOIN tbl_slot slot2 ON xref_intent_slot.slot_id=slot2.id AND slot2.name='Chevrolet'
LEFT JOIN tbl_slot slot3 ON xref_intent_slot.slot_id=slot3.id AND slot3.name='Corvette'
WHERE `intent_id` = (SELECT id from tbl_intent WHERE `name` = 'Car') AND
(slot1.name = '2018' OR
slot2.name = 'Chevrolet' OR
slot3.name = 'Corvette')
group by output_id
HAVING (count(output_id) = 3)
)
LIMIT 0,1

Select and Update Tables in mysql in same mysql statement

I have mysql query :-
select id,CustomerName, Scenario,StepNo,InTransit,IsAlef,Min(RunNo) as run,IsDominant,IsActive from RequestInfo
group by CustomerName, Scenario,StepNo,InTransit,IsAlef,RunNo
having concat(CustomerName,Scenario,StepNo,InTransit,IsAlef,count(RunNo)) in
(select concat(CustomerName,Scenario,StepNo,InTransit,IsAlef,Min(Total)) from
(select CustomerName, Scenario,StepNo,InTransit,IsAlef,RunNo, count(RunNo) Total from RequestInfo
group by CustomerName, Scenario,StepNo,InTransit,IsAlef,RunNo
) b
group by CustomerName, Scenario,StepNo,InTransit,IsAlef);
which gives output :-
+------+--------------+--------------+--------+-----------+--------+------+------------+----------+
| id | CustomerName | Scenario | StepNo | InTransit | IsAlef | run | IsDominant | IsActive |
+------+--------------+--------------+--------+-----------+--------+------+------------+----------+
| 1 | Cleartrip | SearchFlight | 1 | No | No | 1 | NULL | NULL |
| 327 | HotStar | SearchTv | 1 | No | No | 1 | NULL | NULL |
| 836 | NDTV | LiveTv | 1 | No | No | 2 | NULL | NULL |
| 1090 | YATRA | SearchFlight | 1 | No | No | 2 | NULL | NULL |
+------+--------------+--------------+--------+-----------+--------+------+------------+----------+
i want to set the last two columns as "Yes" only for the rows which are been shown in the above result.

MySQL - Query returning results in wrong order

The following MySQL queries below are returning results in different orders despite the "ORDER BY" being the same for both queries:
TABLE STRUCTURE
+-----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+----------------+
| image_id | int(10) | NO | PRI | NULL | auto_increment |
| property_id | int(10) | NO | | 0 | |
| image_title | text | NO | | NULL | |
| image_title_id | int(10) | NO | | 0 | |
| image_index | smallint(3) | NO | | 0 | |
| image_version | tinyint(1) | NO | | 2 | |
| image_landscape | tinyint(1) | NO | | 1 | |
| image_visible | tinyint(1) | NO | | 1 | |
| image_type | tinyint(1) | NO | | 3 | |
+-----------------+-------------+------+-----+---------+----------------+
TEST 1
Query:
SELECT image_id, room_text
FROM property_record_images
INNER JOIN property_data_rooms ON property_record_images.image_title_id = property_data_rooms.room_id
WHERE property_id = 1029
ORDER BY image_index
Result:
+----------+-----------------+
| image_id | room_text |
+----------+-----------------+
| 2042 | Front elevation |
| 2043 | Garden to rear |
| 2044 | Kitchen |
| 2045 | Breakfast area |
| 2046 | Lounge |
| 2047 | Master bedroom |
| 2048 | Studio |
+----------+-----------------+
TEST 2
Query:
SELECT GROUP_CONCAT(CONCAT(property_record_images.image_id) SEPARATOR '|')
FROM property_record_images
INNER JOIN property ON property_record_images.property_id = property.property_id
WHERE property_record_images.property_id = 1029
ORDER BY image_index
Result:
+---------------------------------------------------------------------+
| GROUP_CONCAT(CONCAT(property_record_images.image_id) SEPARATOR '|') |
+---------------------------------------------------------------------+
| 2048|2047|2044|2045|2046|2043|2042 |
+---------------------------------------------------------------------+
This is occurring with random records (different "property_id") so it's not an simple as just reversing the ORDER BY for the second query.
Any idea why this is happening and where I have gone wrong with the query?
see http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat
I think you should get ordered group concat by:
SELECT GROUP_CONCAT(CONCAT(property_record_images.image_id) ORDER BY image_index SEPARATOR '|')
FROM property_record_images
INNER JOIN property ON property_record_images.property_id = property.property_id
WHERE property_record_images.property_id = 1029
ORDER BY image_index

MySQL Recursive Select Query with unknown number of result set columns

I have been looking for a solution for the last few hours.
Here is some data to help explain this problem:
mysql> describe REGION_FEATURE;
+-----------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+------------------+------+-----+---------+----------------+
| ID | int(10) unsigned | NO | PRI | NULL | auto_increment |
| REGION_ID | int(10) unsigned | NO | MUL | NULL | |
| NAME | varchar(255) | NO | | NULL | |
| FILENAME_NOMENCLATURE | varchar(255) | NO | | NULL | |
| CONFIG_FILE_TYPE | int(10) unsigned | NO | | NULL | |
| ITERATOR_GROUP | int(10) unsigned | NO | | NULL | |
+-----------------------+------------------+------+-----+---------+----------------+
mysql> select * from REGION_FEATURE;
+----+-----------+----------------+-----------------------+------------------+----------------+
| ID | REGION_ID | NAME | FILENAME_NOMENCLATURE | CONFIG_FILE_TYPE | ITERATOR_GROUP |
+----+-----------+----------------+-----------------------+------------------+----------------+
| 1 | 1 | MaxCpe01 | c01 | 0 | 1 |
| 2 | 1 | MaxCpe05 | c05 | 0 | 1 |
| 3 | 1 | PrivacyEnable | pe | 0 | 2 |
| 4 | 1 | PrivacyDisable | pd | 0 | 2 |
I need to write a query that will give the result in this way. I am expecting 4 rows, but the N number of combinations.
rows with the same interator_group(I_G) are not combined. In this example it's I_G =1 x I_G=2 but there can be N number of I_G
I_G=1 x I_G=n x I_G=...
MaxCpe01 | PrivacyEnable
MaxCpe01 | PrivacyDisable
MaxCpe05 | PrivacyEnable
MaxCpe05 | PrivacyDisable
Can anyone help?
Are you looking for something like this?
SELECT t1.name name1, t2.name name2
FROM region_feature t1 CROSS JOIN region_feature t2
WHERE t1.iterator_group <> t2.iterator_group
AND t1.id < t2.id
ORDER BY name1;
Output:
| NAME1 | NAME2 |
|----------|----------------|
| MaxCpe01 | PrivacyDisable |
| MaxCpe01 | PrivacyEnable |
| MaxCpe05 | PrivacyDisable |
| MaxCpe05 | PrivacyEnable |
Here is SQLFiddle demo

Help with MySQL join 3 Tables

I am working on a registration system that uses the following tables:
Person (name etc.), Course (course date), Registration (association table pid, cid)
Person
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | text | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
Example Data:
select * from person;
+-----+--------+
| id | name |
+-----+--------+
| 101 | Graham |
| 102 | Lisa |
| 103 | John |
+-----+--------+
Course
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | text | YES | | NULL | |
| date | date | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
select * from course;
+----+---------+------------+
| id | name | date |
+----+---------+------------+
| 1 | Hip Hop | 2011-06-08 |
| 2 | Dancing | 2006-06-23 |
| 3 | Running | 2007-07-08 |
+----+---------+------------+
Registration
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| pid | int(11) | YES | | NULL | |
| cid | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
select * from registration;
+----+------+------+
| id | pid | cid |
+----+------+------+
| 1 | 101 | 1 |
| 2 | 101 | 2 |
| 3 | 103 | 2 |
+----+------+------+
I would like to find person(s) that have no registration records within the past two years. I am attempting to join the tables based on date calculation but it does not seem to work this way. Is this possible with mysql or is my approach of trying this with one query wrong?
query I have come up with:
select * from
(person left join registration on person.id = registration.pid)
left join course on course.id = registration.cid
AND DATE_FORMAT(`course`.`date`, "%m.%Y") > DATE_FORMAT( DATE_SUB(NOW(), INTERVAL 2 YEAR),"%m.%Y")
WHERE
registration.id IS NULL;
+-----+------+------+------+------+------+------+------+
| id | name | id | pid | cid | id | name | date |
+-----+------+------+------+------+------+------+------+
| 102 | Lisa | NULL | NULL | NULL | NULL | NULL | NULL |
+-----+------+------+------+------+------+------+------+
It should list person 102 and 103 since both registrations are older than 2 years and no other records of newer course dates can be found...
Give this a shot, using a NOT EXISTS clause:
select p.* from person p
where not exists (select 1 from person px
join registration rx on px.id = rx.pid
join course cx on rx.cid = cx.id
where px.id = p.id
and cx.date > DATE_SUB(NOW(), INTERVAL 2 YEAR))