MySQL Query into single multi table join - mysql

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

Related

fill at once data null on table sql

I have table transaksi:
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(150) | YES | | NULL | |
| price | int(11) | YES | | NULL | |
| type | int(11) | YES | | NULL | |
+-------------+--------------+------+-----+---------+-------+
and have data (I fill in the data with insert into transaksi (name,price,type) select name,price,type from store)
+-----------+--------------------------+---------+-------+
| id | name | price | Type |
+-----------+--------------------------+---------+-------+
| NULL | Pants | 79 | 9 |
| NULL | Cup | 38 | 7 |
| NULL | Shoes | 21 | 1 |
| NULL | Hat | 11 | 5 |
| NULL | Pulpen | 39 | 2 |
+___________|__________________________|_________|_______|
How to fill "NULL" data at once in ID ?

Calculate difference between two row amount in MySQL

I am having a table with which calculates balance, where I have these columns in the table:
> describe tbl_credit_log
+------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| visitor_id | int(10) unsigned | YES | MUL | NULL | |
| post_owner_id | int(10) unsigned | YES | MUL | NULL | |
| post_id | int(10) unsigned | YES | MUL | NULL | |
| balance | double(50,6) | YES | | NULL | |
| credits | double(50,6) | YES | | NULL | |
| debits | double(50,6) | YES | | NULL | |
| trans_id | varchar(255) | NO | | NULL | |
| trans_amount | double(50,6) | YES | | NULL | |
| earningtype | varchar(15) | NO | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+------------------+------------------+------+-----+---------+----------------+
Where here is the few entries from which I want to do the calculations :
+---------+------------+---------------+---------+------------------+-------------+-----------+--------+---------------------+
| id | visitor_id | post_owner_id | post_id | advertisement_id | balance | credits | debits | created_at |
+---------+------------+---------------+---------+------------------+-------------+-----------+--------+---------------------+
| 4331666 | 11006 | 11 | NULL | NULL | 3639.624400 | 22.500000 | NULL | 2018-08-10 05:45:37 |
| 4364034 | 7206 | 11 | NULL | NULL | 5139.607900 | 22.500000 | NULL | 2018-08-10 11:02:52 |
| 4377238 | 4353 | 11 | NULL | NULL | 5162.107900 | 22.500000 | NULL | 2018-08-10 12:52:01 |
| 4485288 | 9664 | 11 | NULL | NULL | 5184.607900 | 22.500000 | NULL | 2018-08-11 08:58:19 |
| 4544185 | 11709 | 11 | NULL | NULL | 5207.107900 | 22.500000 | NULL | 2018-08-11 19:06:52 |
| 4550728 | 11970 | 11 | NULL | NULL | 5229.607900 | 22.500000 | NULL | 2018-08-11 20:39:36 |
| 4607317 | 12021 | 11 | NULL | NULL | 5252.107900 | 22.500000 | NULL | 2018-08-12 07:29:17 |
| 4629660 | 11926 | 11 | NULL | NULL | 5274.607900 | 22.500000 | NULL | 2018-08-12 09:18:56 |
| 4725299 | 12088 | 11 | NULL | NULL | 5297.107900 | 22.500000 | NULL | 2018-08-13 01:54:53 |
| 4725347 | 10253 | 11 | NULL | NULL | 5319.607900 | 22.500000 | NULL | 2018-08-13 08:29:46 |
| 4725357 | 12140 | 11 | NULL | NULL | 5342.107900 | 22.500000 | NULL | 2018-08-13 09:27:44 |
+---------+------------+---------------+---------+------------------+-------------+-----------+--------+---------------------+
here we have difference (more then 50) in amount on id number4364034 which I want to find in whole database where user id will be post_owner_id as foreign key.
I want to get all the post_owner_id who have difference in the amount and their created_at.
So the expected results will be something like this
+---------+------------+---------------+---------+------------------+-------------+-----------+--------+---------------------+
| id | visitor_id | post_owner_id | post_id | advertisement_id | balance | credits | debits | created_at |
+---------+------------+---------------+---------+------------------+-------------+-----------+--------+---------------------+
| 4364034 | 7206 | 11 | NULL | NULL | 5139.607900 | 22.500000 | NULL | 2018-08-10 11:02:52 |
+---------+------------+---------------+---------+------------------+-------------+-----------+--------+---------------------+
here we will have multiple post_owner_id. I am showing one post_owner_id because this result is only of one post owner id. But I want to get different post owner id who have difference balance is more then 50.
I am not able to figure out how can I do this. I want to compare prices on group_by post_owner_id and get the row where the difference amount is.
Consider the following:
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(id SERIAL PRIMARY KEY
,balance DECIMAL(12,6) NOT NULL
,created_at timestamp NOT NULL
);
INSERT INTO my_table VALUES
(4331666,3639.624400,'2018-08-10 05:45:37'),
(4364034,5139.607900,'2018-08-10 11:02:52'),
(4377238,5162.107900,'2018-08-10 12:52:01'),
(4485288,5184.607900,'2018-08-11 08:58:19'),
(4544185,5207.107900,'2018-08-11 19:06:52'),
(4550728,5229.607900,'2018-08-11 20:39:36'),
(4607317,5252.107900,'2018-08-12 07:29:17'),
(4629660,5274.607900,'2018-08-12 09:18:56'),
(4725299,5297.107900,'2018-08-13 01:54:53'),
(4725347,5319.607900,'2018-08-13 08:29:46'),
(4725357,5342.107900,'2018-08-13 09:27:44');
Option 1:
SELECT a.*
FROM
( SELECT x.*
, MIN(y.id) y_id
FROM my_table x
JOIN my_table y
ON y.created_at > x.created_at
GROUP
BY x.id
) a
JOIN my_table b
ON b.id = a.y_id
AND b.balance > a.balance + 50;
+---------+-------------+---------------------+---------+
| id | balance | created_at | y_id |
+---------+-------------+---------------------+---------+
| 4331666 | 3639.624400 | 2018-08-10 05:45:37 | 4364034 |
+---------+-------------+---------------------+---------+
Option 2:
SELECT id
, balance
, created_at
FROM
( SELECT x.*
, #i<balance-50 i
, #i:=balance
FROM my_table x
, (SELECT #i:=null) vars
ORDER
BY created_at
) n
WHERE i = 1;
+---------+-------------+---------------------+
| id | balance | created_at |
+---------+-------------+---------------------+
| 4364034 | 5139.607900 | 2018-08-10 11:02:52 |
+---------+-------------+---------------------+
You can adapt either of the techniques above to return either row

Select from two tables based on table primary key

I have a table representing common values for two types of records and I have another two tables to hold the data that are different from each other. The table 1 is as follows
banking
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| officer | varchar(32) | NO | | NULL | |
| bank | varchar(64) | NO | | NULL | |
| branch | varchar(64) | NO | | NULL | |
| amount | int(11) | NO | | NULL | |
| date | date | NO | | NULL | |
| sys_date | date | NO | | NULL | |
| source_document | varchar(360) | NO | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
machinery_banking
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| banking_id | int(11) | NO | | NULL | |
| engine_no | varchar(64) | NO | | NULL | |
| chassis_no | varchar(64) | NO | | NULL | |
| receipt_number | varchar(64) | NO | | NULL | |
| payment_type | int(11) | NO | | NULL | |
+----------------+-------------+------+-----+---------+-------+
spare_parts_banking
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| banking_id | int(11) | NO | | NULL | |
| invoice_number | varchar(32) | NO | | NULL | |
+----------------+-------------+------+-----+---------+-------+
Each data that is entered to banking table has the other data in one of each tables machinery_banking or the spare_parts_banking.
I have the following data in the banking table
+----+---------+------------+------------+-----------+------------+------------+------------------------------------------------------------------------------------------+
| id | officer | bank | branch | amount | date | sys_date | source_document |
+----+---------+------------+------------+-----------+------------+------------+------------------------------------------------------------------------------------------+
| 1 | prasad | Sampath | Kaduruwela | 234234234 | 2017-06-28 | 2017-06-28 | image |
| 2 | prasad | Commercial | Colombo | 234234234 | 2017-05-28 | 2017-05-28 | image |
+----+---------+------------+------------+-----------+------------+------------+------------------------------------------------------------------------------------------+
I have the following data in the machinery_banking
+------------+-----------+------------+----------------+--------------+
| banking_id | engine_no | chassis_no | receipt_number | payment_type |
+------------+-----------+------------+----------------+--------------+
| 1 | 2324234 | NL234234 | RAN234 | 1 |
+------------+-----------+------------+----------------+--------------+
Data in the spare_parts_banking is as follows
+------------+----------------+
| banking_id | invoice_number |
+------------+----------------+
| 2 | INVRAN1 |
+------------+----------------+
I have tried the following query
SELECT a.id, a.officer, a.bank, a.branch, a.amount, a.date, a.sys_date, b.engine_no, b.chassis_no, a.source_document, c.invoice_number
FROM banking a,machinery_banking b, spare_parts_banking c
WHERE a.id = b.banking_id OR a.id = c.banking_id;
and ended up the following result
+----+---------+------------+------------+-----------+------------+------------+-----------+------------+------------------------------------------------------------------------------------------+----------------+
| id | officer | bank | branch | amount | date | sys_date | engine_no | chassis_no | source_document | invoice_number |
+----+---------+------------+------------+-----------+------------+------------+-----------+------------+------------------------------------------------------------------------------------------+----------------+
| 1 | prasad | Sampath | Kaduruwela | 234234234 | 2017-06-28 | 2017-06-28 | 2324234 | NL234234 | http://res.cloudinary.com/randeepa-com/image/upload/v1498455394/dmhxqal8hjcthhgav0n9.jpg | INVRAN1 |
| 2 | prasad | Commercial | Colombo | 234234234 | 2017-05-28 | 2017-05-28 | 2324234 | NL234234 | http://res.cloudinary.com/randeepa-com/image/upload/v1498455394/dmhxqal8hjcthhgav0n9.jpg | INVRAN1 |
+----+---------+------------+------------+-----------+------------+------------+-----------+------------+------------------------------------------------------------------------------------------+----------------+
The result I wish to obtain is as follows
+----+---------+------------+------------+-----------+------------+------------+-----------+------------+------------------------------------------------------------------------------------------+----------------+
| id | officer | bank | branch | amount | date | sys_date | engine_no | chassis_no | source_document | invoice_number |
+----+---------+------------+------------+-----------+------------+------------+-----------+------------+------------------------------------------------------------------------------------------+----------------+
| 1 | prasad | Sampath | Kaduruwela | 234234234 | 2017-06-28 | 2017-06-28 | 2324234 | NL234234 | http://res.cloudinary.com/randeepa-com/image/upload/v1498455394/dmhxqal8hjcthhgav0n9.jpg | |
| 2 | prasad | Commercial | Colombo | 234234234 | 2017-05-28 | 2017-05-28 | | | http://res.cloudinary.com/randeepa-com/image/upload/v1498455394/dmhxqal8hjcthhgav0n9.jpg | INVRAN1 |
+----+---------+------------+------------+-----------+------------+------------+-----------+------------+------------------------------------------------------------------------------------------+----------------+
What query can I use to get this result
The reason, why you have data duplication is because of the OR between conditions
I believe this should work
SELECT a.id,
a.officer,
a.bank,
a.branch,
a.amount,
a.date,
a.sys_date,
b.engine_no,
b.chassis_no,
a.source_document,
c.invoice_number
FROM banking a
LEFT JOIN machinery_banking b
ON a.id = b.banking_id
LEFT JOIN spare_parts_banking c
ON a.id = c.banking_id;
Also move to new JOIN syntax, it is much easier to comprehend.
SQL Join Syntax - Reference
Try this:
SELECT a.id, a.officer, a.bank, a.branch, a.amount, a.date, a.sys_date, b.engine_no, b.chassis_no, a.source_document, c.invoice_number
FROM banking a,machinery_banking b, spare_parts_banking c
WHERE a.id = b.banking_id(+)
AND a.id = c.banking_id(+);

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

MySQL db join query

I am working with an Employee database in Mysql. My Db contains the following tables
mysql> describe edept;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| dept | varchar(20) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
mysql>describe esal;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| basic | int(11) | NO | | NULL | |
| pf | int(11) | NO | | NULL | |
+-------+---------+------+-----+---------+-------+
mysql> describe edesig;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| desig | varchar(20) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
mysql> select * from edetails inner join edept on edetails.dept=edept.id;
+----+--------+-----+------+-------+-------+----+----+------------------+
| id | name | age | dept | desig | basic | pf | id | dept |
+----+--------+-----+------+-------+-------+----+----+------------------+
| 1 | swetha | 21 | 3 | 2 | 2 | 2 | 3 | Business Process |
+----+--------+-----+------+-------+-------+----+----+------------------+
mysql> describe edetails;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | NO | | NULL | |
| age | int(11) | NO | | NULL | |
| dept | int(11) | NO | MUL | NULL | |
| desig | int(11) | YES | MUL | NULL | |
| basic | int(11) | NO | MUL | NULL | |
| pf | int(11) | NO | MUL | NULL | |
+-------+-------------+------+-----+---------+-------+
I have to get values for dept,desig,basic,pf from the tables edept.dept,edesig.desig,esal.basic,esal.pf respectively.
I used foreign keys for all the fields for which i have to retrieve values from other tables.And i tried a sample inner join query. but i got the output as follows:
mysql> select * from edetails inner join edept on edetails.dept=edept.id;
+----+--------+-----+------+-------+-------+----+----+------------------+
| id | name | age | dept | desig | basic | pf | id | dept |
+----+--------+-----+------+-------+-------+----+----+------------------+
| 1 | swetha | 21 | 3 | 2 | 2 | 2 | 3 | Business Process |
+----+--------+-----+------+-------+-------+----+----+------------------+
My edept table contains the following:
mysql> select * from edept;
+----+------------------+
| id | dept |
+----+------------------+
| 3 | Business Process |
+----+------------------+
How can i eliminate duplicate columns. i need the value "business process" in the dept field of the edept table
Try this::
select
edetails.id,
edetails.name,
edetails.age,
edetails.dept,
edesig.desig,
edetails.basic,
edetails.pf,
edept.dept
from edetails
inner join edept on edetails.dept=edept.id
INNER JOIN edesig on edesig.id=edetails.desig