I have a test table
mysql> select * from test1 limit 20;
+------+------+----------------------------------+
| id | num | pass |
+------+------+----------------------------------+
| 1 | 1 | c4ca4238a0b923820dcc509a6f75849b |
| 2 | 3 | c81e728d9d4c2f636f067f89cc14862c |
| 1 | 1 | c4ca4238a0b923820dcc509a6f75849b |
| 2 | 2 | c81e728d9d4c2f636f067f89cc14862c |
| 1 | 1 | c4ca4238a0b923820dcc509a6f75849b |
| 2 | 3 | c81e728d9d4c2f636f067f89cc14862c |
| 3 | 3 | eccbc87e4b5ce2fe28308fd9f2a7baf3 |
| 4 | 6 | a87ff679a2f3e71d9181a67b7542122c |
| 5 | 7 | e4da3b7fbbce2345d7772b0674a318d5 |
| 6 | 6 | 1679091c5a880faf6fb5e6087eb1b2dc |
| 7 | 8 | 8f14e45fceea167a5a36dedd4bea2543 |
| 8 | 11 | c9f0f895fb98ab9159f51fd0297e236d |
| 9 | 10 | 45c48cce2e2d7fbdea1afc51c7c6ad26 |
| 10 | 12 | d3d9446802a44259755d38e6d163e820 |
| 11 | 12 | 6512bd43d9caa6e02c990b0a82652dca |
| 12 | 18 | c20ad4d76fe97759aa27a0c99bff6710 |
...
I want to delete the 1st,3rd and 9th row.So I use such command
delete from test1 limit 1,3,9;
But it doesn't work,is there any workaround can do this?
Related
I have a query_table Table and wants to join with match_table Table with nearest matching string. If it was vice-versa then 'like' would have worked but have no idea how to do this.
query_table
+----+------------------+
| id | string |
+----+------------------+
| 1 | fcc9e8796feb |
| 2 | fcdbd7ebcf89 |
| 3 | fccc87896feb |
| 4 | fcc7c7896fef |
| 5 | fcced777aaaf |
+----+------------------+
match_table
+----+-----------+
| id | match_code|
+----+-----------+
| 1 | fcff |
| 2 | fcccc |
| 3 | fccc8 |
| 4 | fccc9 |
| 5 | fccdb |
| 6 | fccdc |
| 7 | fccd8 |
| 8 | fcce |
| 9 | fcced |
| 10 | fccee |
| 11 | fcce6 |
| 12 | fcc7b |
| 13 | fcc7c |
| 14 | fcc8e |
| 15 | fcc87 |
| 16 | fcc88 |
| 17 | fcc9e |
| 18 | fcdbb |
| 19 | fcdbc |
| 20 | fcdbd |
+----+-----------+
I expect
result
+----+------------------+----+----------------+
| id | string | id | match_code |
+----+------------------+----+----------------
| 1 | fcc9e8796feb | 17 | fcc9e |
| 2 | fcdbd7ebcf89 | 20 | fcdbd |
| 3 | fccc87896feb | 3 | fccc8 |
| 4 | fcc7c7896fef | 13 | fcc7c |
| 5 | fcced777aaaf | 9 | fcced |
+----+------------------+----+----------------+
How to calculate opening and closing stocks for particular date range
I want to calculate opening and closing stocks for items for a provided date range.
I have tables as follows,
global_items:
+-----+--------+
| id | name |
+-----+--------+
| 1 | item 1 |
+-----+--------+
| 2 | item 2 |
+-----+--------+
| 3 | item 3 |
+-----+--------+
| 4 | item 4 |
+-----+--------+
| ... | ... |
+-----+--------+
my_items:
+-----+----------------+-----------+
| id | global_item_id | outlet_id |
+-----+----------------+-----------+
| 1 | 1 | 7 |
+-----+----------------+-----------+
| 2 | 2 | 7 |
+-----+----------------+-----------+
| 3 | 3 | 7 |
+-----+----------------+-----------+
| 4 | 4 | 7 |
+-----+----------------+-----------+
| ... | ... | ... |
+-----+----------------+-----------+
my_item_stocks:
+-----+------------+---------+---------------+-----------+---------------+-------+
| id | my_item_id | size_id | purchase_rate | sale_rate | opening_stock | stock |
+-----+------------+---------+---------------+-----------+---------------+-------+
| 1 | 1 | 10 | 100 | 200 | 0 | 0 |
+-----+------------+---------+---------------+-----------+---------------+-------+
| 2 | 1 | 11 | 100 | 200 | 0 | 5 |
+-----+------------+---------+---------------+-----------+---------------+-------+
| 3 | 2 | 10 | 100 | 200 | 1.05 | 1.05 |
+-----+------------+---------+---------------+-----------+---------------+-------+
| 4 | 3 | 12 | 100 | 200 | 10 | 10 |
+-----+------------+---------+---------------+-----------+---------------+-------+
| ... | ... | ... | 100 | 200 | 0 | 1 |
+-----+------------+---------+---------------+-----------+---------------+-------+
sizes:
+-----+--------+----------+
| id | name | quantity |
+-----+--------+----------+
| 10 | 750 ml | 750 |
+-----+--------+----------+
| 11 | 500 ml | 500 |
+-----+--------+----------+
| 12 | 350 ml | 350 |
+-----+--------+----------+
| ... | ... | ... |
+-----+--------+----------+
Then I have sales and purchases tables as follows, adding purchase updates the stock in my_item_stocks and adding sale reduces stock.
purchases:
+-----+------------+-----------+
| id | date | outlet_id |
+-----+------------+-----------+
| 1 | 2018-07-01 | 7 |
+-----+------------+-----------+
| 2 | 2018-07-10 | 7 |
+-----+------------+-----------+
| 3 | 2018-07-19 | 7 |
+-----+------------+-----------+
| ... | ... | ... |
+-----+------------+-----------+
purchase_items:
+-----+-------------+------------+---------+----------+
| id | purchase_id | my_item_id | size_id | quantity |
+-----+-------------+------------+---------+----------+
| 1 | 1 | 1 | 10 | 2 |
+-----+-------------+------------+---------+----------+
| 2 | 1 | 2 | 11 | 5 |
+-----+-------------+------------+---------+----------+
| 3 | 2 | 2 | 10 | 17 |
+-----+-------------+------------+---------+----------+
| 4 | 3 | 2 | 12 | 15 |
+-----+-------------+------------+---------+----------+
| 5 | 3 | 2 | 12 | 10 |
+-----+-------------+------------+---------+----------+
| ... | ... | ... | ... | ... |
+-----+-------------+------------+---------+----------+
sales:
+-----+------------+-----------+
| id | date | outlet_id |
+-----+------------+-----------+
| 1 | 2018-07-01 | 7 |
+-----+------------+-----------+
| 2 | 2018-07-10 | 7 |
+-----+------------+-----------+
| 3 | 2018-07-19 | 7 |
+-----+------------+-----------+
| ... | ... | ... |
+-----+------------+-----------+
sale_items:
+-----+-------------+------------+---------+------+
| id | sale_id | my_item_id | size_id | quantity |
+-----+---------+------------+---------+----------+
| 1 | 1 | 1 | 10 | 1 |
+-----+---------+------------+---------+----------+
| 2 | 1 | 2 | 11 | 2 |
+-----+---------+------------+---------+----------+
| 3 | 2 | 2 | 10 | 10 |
+-----+---------+------------+---------+----------+
| 4 | 3 | 2 | 12 | 5 |
+-----+---------+------------+---------+----------+
| 5 | 3 | 2 | 12 | 2 |
+-----+---------+------------+---------+----------+
| ... | ... | ... | ... | ... |
+-----+---------+------------+---------+----------+
Now for selected date range e.g. from 2018-07-01 to 2018-07-19 I want the output like below,
+-----------+--------------------------+--------------------------+--------------------------+--------------------------+
| Item Name | Opening Stock | Purchase | Sale | Closing Stock |
+ +--------------------------+--------------------------+--------------------------+--------------------------+
| | 750 ml | 500 ml | 350 ml | 750 ml | 500 ml | 350 ml | 750 ml | 500 ml | 350 ml | 750 ml | 500 ml | 350 ml |
+-----------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
| Item 1 | | | | 2 | 5 | | 1 | 2 | | 3 | 7 | |
+-----------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
| Item 2 | 1.05 | | | 17 | | | 10 | | | 8.05 | | |
+-----------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
+-----------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
I am not sql pro, how can I achieve this? I am confused because of lot of related tables, just want right direction.
I am doing this is laravel so please suggest even if there is any way to achieve this using eloquent.
Models: GlobalItem, MyItem, MyItemStock, Size, Purchase, PurchaseItem, Sale, SaleItem
Many thanks!
first table :
table Name : ssc_course;
+---------------+-----------------+------------+
| ssc_course_id | ssc_course_name | qualify_id |
+---------------+-----------------+------------+
| 1 | HSC | 1 |
| 2 | Diploma | 1 |
| 3 | ITI | 1 |
+---------------+-----------------+------------+
second table :
table name :ssc_stream
+---------------+--------------------+-----------------+
| ssc_stream_id | ssc_stream_name | ssc_course_id(fk) |
+---------------+--------------------+-----------------+
| 1 | Science | 1 |
| 2 | Commers | 1 |
| 3 | Arts | 1 |
| 17 | Computer Engg | 2 |
| 18 | Mechanical Engg | 2 |
| 19 | Civil Engg | 2 |
| 20 | Electronics & TELE | 2 |
| 21 | E&TC | 2 |
+---------------+--------------------+-----------------+
third table :(master table)
table name : ssc_college
+------------+--------------+---------------+---------------+-
| ssc_clg_id | clg_login_id | ssc_stream_id | ssc_course_id |
+------------+--------------+---------------+---------------+-
| 9 | 2 | 1 | 1 |
| 10 | 2 | 1 | 1 |
| 11 | 2 | 2 | 1 |
| 12 | 2 | 2 | 1 |
| 13 | 2 | 3 | 1 |
| 14 | 2 | 3 | 1 |
| 15 | 3 | 1 | 1 |
| 16 | 3 | 1 | 1 |
| 17 | 3 | 2 | 1 |
| 18 | 3 | 2 | 1 |
| 19 | 3 | 3 | 1 |
| 20 | 3 | 3 | 1 |
| 21 | 4 | 1 | 1 |
| 22 | 4 | 1 | 1 |
| 23 | 4 | 2 | 1 |
| 24 | 4 | 2 | 1 |
| 25 | 4 | 3 | 1 |
| 26 | 4 | 3 | 1 |
| 27 | 5 | 17 | 2 |
| 28 | 5 | 17 | 2 |
| 29 | 5 | 18 | 2 |
| 30 | 5 | 18 | 2 |
| 31 | 5 | 19 | 2 |
| 32 | 5 | 19 | 2 |
| 33 | 5 | 20 | 2 |
| 34 | 5 | 20 | 2 |
| 35 | 5 | 21 | 2 |
| 36 | 5 | 21 | 2 |
| 38 | 6 | 17 | 2 |
| 39 | 6 | 17 | 2 |
| 40 | 6 | 18 | 2 |
*************************************************************
I am trying to save course,stream id's into ssc_college table.
1 course have multiple stream how can i insert this type of values
ex. course 1-diploma and this course have multiple stream like-
1.comp. engg , 2.mechanical ,3.e&tc i want to add course id and
stream id at a time of third (ssc_college) table.
and multiple stream for multiple course how i can add at a time
multiple parent and multiple child id in mysql table.
Note: stream are dependant of course table.
I need to create a log having the purchase date of an item.
Items can be owned by only one buyer at time. So, for example, if item1 was purchased by buyer2 in 2009 and after by buyer1 in 2015, then between 2009 and 2015 was owned by buyer2.
Here is my table:
+--------+------------+-----------+----------+
| id_doc | date | id_item | id_buyer |
+--------+------------+-----------+----------+
| 11 | 2016-06-07 | 1 | 4 |
| 10 | 2016-06-06 | 1 | 4 |
| 1 | 2015-11-30 | 1 | 1 |
| 9 | 2009-01-01 | 1 | 2 |
| 4 | 2001-01-12 | 1 | 2 |
| 8 | 1996-06-06 | 1 | 2 |
| 3 | 1995-05-29 | 1 | 1 |
| 2 | 1998-05-23 | 2 | 2 |
| 7 | 2014-10-10 | 3 | 2 |
| 6 | 2003-12-12 | 3 | 3 |
| 5 | 1991-01-12 | 3 | 2 |
+--------+------------+-----------+----------+
Here is a kind of table/view I need:
+------------+------------+-----------+----------+--------+
| date_from | date_to | id_item | id_buyer | id_doc |
+------------+------------+-----------+----------+--------+
| 2016-06-07 | - | 1 | 4 | 11 |
| 2016-06-06 | 2016-06-07 | 1 | 4 | 10 |
| 2015-11-30 | 2016-06-06 | 1 | 1 | 1 |
| 2009-01-01 | 2015-11-30 | 1 | 2 | 9 |
| 2001-01-12 | 2009-01-01 | 1 | 2 | 4 |
| 1996-06-06 | 2001-01-12 | 1 | 2 | 8 |
| 1995-05-29 | 1996-06-06 | 1 | 1 | 3 |
| 1998-05-23 | - | 2 | 2 | 2 |
| 2014-10-10 | - | 3 | 2 | 7 |
| 2003-12-12 | 2014-10-10 | 3 | 3 | 6 |
| 1991-01-12 | 2003-12-12 | 3 | 2 | 5 |
+------------+------------+-----------+----------+--------+
I've tried a lot with GROUP BY, GROUP_CONCAT, trying to access next record date, etc ... but I can't found out how to solve the problem.
Thanks in advance.
I finally found out the solution only for past purchases.
SELECT
main.id_doc, main.id_item, main.date AS "date_from", bi.date AS "date_to", main.id_buyer
FROM
MyTable main, MyTable bi
WHERE
bi.id_doc =
(
SELECT sub.id_doc
FROM MyTable sub
WHERE sub.id_item = main.id_item AND sub.date > main.date ORDER BY sub.date ASC LIMIT 1
);
I have three table
td_product
|--------------|------------------------------------------------------------|
| product_id | product_title | compatible_model | |
|---------------------------------------------------------------------------|
| 1 | prod-1 | 1,4,5,6,8 | |
|---------------------------------------------------------------------------|
| 2 | prod-2 | 1,5,6 | |
|---------------------------------------------------------------------------|
| 3 | prod-3 | 4,6 | |
|---------------------------------------------------------------------------|
the i have
td_model
|--------------|----------------------------------------------------|
| model_id | model_title | brand_id | model_slug |
|-------------------------------------------------------------------|
| 1 | mode-1 | 1 | 1-mode-1 |
|-------------------------------------------------------------------|
| 2 | mode-2 | 2 | 2-mode-2 |
|-------------------------------------------------------------------|
| 3 | mode-3 | 4 | 3-mode-3 |
|-------------------------------------------------------------------|
| 4 | mode-4 | 4 | 4-mode-4 |
|-------------------------------------------------------------------|
| 5 | mode-5 | 2 | 5-mode-5 |
|-------------------------------------------------------------------|
| 6 | mode-6 | 4 | 6-mode-6 |
|-------------------------------------------------------------------|
| 7 | mode-7 | 1 | 7-mode-7 |
|-------------------------------------------------------------------|
| 8 | mode-8 | 2 | 8-mode-8 |
|-------------------------------------------------------------------|
and finally the brand
|--------------|------------------------------------------------------------|
| brand_id | brand_title | |
|---------------------------------------------------------------------------|
| 1 | brnd-1 | |
|---------------------------------------------------------------------------|
| 2 | brnd-2 | |
|---------------------------------------------------------------------------|
| 3 | brnd-3 | |
|---------------------------------------------------------------------------|
| 4 | brnd-3 | |
|---------------------------------------------------------------------------|
| 5 | brnd-3 | |
|---------------------------------------------------------------------------|
| 6 | prod-3 | |
|---------------------------------------------------------------------------|
Now i have a query which runs like this
SELECT * FROM td_product,td_model,td_brand
WHERE
td_product.product_id > 0
AND td_model.model_id IN (td_product.compatible_model)
AND td_model.brand_id = td_brand.brand_id
AND td_model.model_slug = '1-mode-1'
Which displays prod-1 and prod-2
but this query return null value.
SELECT * FROM td_product,td_model,td_brand
WHERE
td_product.product_id > 0
AND td_model.model_id IN (td_product.compatible_model)
AND td_model.brand_id = td_brand.brand_id
AND td_model.model_slug = '5-mode-5'
What am i doing wrong?
Instead of IN use FIND_IN_SET(...); change:
AND td_model.model_id IN (td_product.compatible_model)
to:
AND FIND_IN_SET(td_model.model_id, td_product.compatible_model) > 0