Mysql search for exact number in field - mysql

in my MySql DB, i have column containing that kind of value
63 61 57 52 50 47 46 44 43 34 33 27 23 22 21 10 5 3 2 1
Those numbers are separated by tab.
Impossible to get the good result for a simple query that would aid something like this
SELECT * FROM mytable WHERE mycolumn = 63
I'm not sure if "=" is the good method, i've also tried LIKE, IN and even FIND_IN_SET
I need some help :)

Related

join same type table data in a table in sql

In the past I entried my data month wise due to lack of my knowledge in the month name table. But now I easily filter them, in my database, there are 6 months (Oct - Feb) table are there with same row name month-wise data can I put all the data in a table, for manually put them in a file little bit difficult for me because of id,
so Please suggest to me to make it easily
for example, the October 2018 table is this
id user_name date nota veet tree location
1 milon 10/10/2018 43 12 111 bandel
2 kadir 11/10/2018 12 34 76 katwa
3 javed 22/10/2018 33 56 92 sirampur
4 milon 29/10/2018 55 21 78 salar
november 2018 table is
id user_name date nota veet tree location
1 milon 10/11/2018 13 12 71 Rampurhat
2 kadir 11/11/2018 12 24 76 katwa
3 javed 12/11/2018 53 30 62 kandi
4 milon 24/11/2018 55 27 58 salar
now I want SQL table like this
id user_name date nota veet tree location
1 milon 10/10/2018 43 12 111 bandel
2 kadir 11/10/2018 12 34 76 katwa
3 javed 22/10/2018 33 56 92 sirampur
4 milon 29/10/2018 55 21 78 salar
5 milon 10/11/2018 13 12 71 Rampurhat
6 kadir 11/11/2018 12 24 76 katwa
7 javed 12/11/2018 53 30 62 kandi
8 milon 24/11/2018 55 27 58 salar
You can create a new table (for example user_locations). Then you can copy data from first table and then copy data from second table. If you have ID column with auto-increment, and if you do not specify the ID column in select, than the ID will be assigned automatically and there will not be collision of IDs. Example of SQL for selecting from one table and inserting to another table:
INSERT INTO user_locations (user_name, date, nota, veet, tree, location)
SELECT user_name, date, nota, veet, tree, location FROM user_locations_november_2018
... however I am not sure that I understand you correctly. What exactly are you trying to do? Move data from all tables to one table (this is the one I answer). Or just select data from all the tables (in which case the UNION from other answer is correct). Or to select all data and put them into a text file?
select * from table1 Union Select * from Table2
use The SQL UNION Operator

MySQL hanging when using UPDATE CASE WHEN for saving reordering list

I'm using MySQL UPDATE CASE WHEN ELSE statement to update an ordered list on my web application. I have a html table with the sortable drag-and-drop plugin, and on the stop event I hit a servlet that persists the new order to the MySQL Table.
The thing is that from time to time MySQL hangs when the user reorders the table very quickly. Like in the example below, there were 4 statements issued in the same second, and then it hanged.
22/01/2014 14:03:25 UPDATE `edited_table_name` SET `order` = CASE NR_DMD WHEN 92059 THEN 22 WHEN 124376 THEN 23 WHEN 124163 THEN 24 WHEN 127019 THEN 25 WHEN 123816 THEN 26 WHEN 124348 THEN 27 WHEN 127017 THEN 28 WHEN 126764 THEN 29 WHEN 126637 THEN 30 WHEN 122170 THEN 31 WHEN 125630 THEN 32 WHEN 125515 THEN 33 WHEN 124487 THEN 34 WHEN 124374 THEN 35 WHEN 124343 THEN 36 WHEN 124327 THEN 37 WHEN 126838 THEN 38 WHEN 126720 THEN 39 WHEN 126718 THEN 40 WHEN 123510 THEN 41 WHEN 122966 THEN 42 WHEN 124385 THEN 43 WHEN 122754 THEN 44 WHEN 124312 THEN 45 WHEN 122311 THEN 46 WHEN 121577 THEN 47 WHEN 121028 THEN 48 WHEN 123354 THEN 49 WHEN 121682 THEN 50 WHEN 120993 THEN 51 WHEN 120860 THEN 52 WHEN 120750 THEN 53 WHEN 120460 THEN 54 WHEN 120459 THEN 55 WHEN 120397 THEN 56 WHEN 119178 THEN 57 WHEN 111723 THEN 58 WHEN 127644 THEN 59 WHEN 127610 THEN 60 WHEN 127609 THEN 61 WHEN 127542 THEN 62 WHEN 125799 THEN 63 ELSE order END WHERE NR_DMD IN(92059,124376,124163,127019,123816,124348,127017,126764,126637,122170,125630,125515,124487,124374,124343,124327,126838,126720,126718,123510,122966,124385,122754,124312,122311,121577,121028,123354,121682,120993,120860,120750,120460,120459,120397,119178,111723,127644,127610,127609,127542,125799)
22/01/2014 14:03:25 UPDATE `edited_table_name` SET `order` = CASE NR_DMD WHEN 121419 THEN 10 WHEN 110378 THEN 11 WHEN 124376 THEN 12 WHEN 124163 THEN 13 WHEN 127019 THEN 14 WHEN 123816 THEN 15 WHEN 124348 THEN 16 WHEN 127017 THEN 17 WHEN 126764 THEN 18 WHEN 126637 THEN 19 WHEN 122170 THEN 20 WHEN 125630 THEN 21 WHEN 125515 THEN 22 WHEN 124487 THEN 23 WHEN 124374 THEN 24 WHEN 124343 THEN 25 WHEN 124327 THEN 26 WHEN 126838 THEN 27 WHEN 126720 THEN 28 WHEN 126718 THEN 29 WHEN 123510 THEN 30 WHEN 122966 THEN 31 WHEN 124385 THEN 32 WHEN 122754 THEN 33 WHEN 124312 THEN 34 WHEN 122311 THEN 35 WHEN 121577 THEN 36 ELSE order END WHERE NR_DMD IN(121419,110378,124376,124163,127019,123816,124348,127017,126764,126637,122170,125630,125515,124487,124374,124343,124327,126838,126720,126718,123510,122966,124385,122754,124312,122311,121577)
22/01/2014 14:03:25 UPDATE `edited_table_name` SET `order` = CASE NR_DMD WHEN 127585 THEN 21 WHEN 124376 THEN 22 WHEN 124163 THEN 23 WHEN 127019 THEN 24 WHEN 123816 THEN 25 WHEN 124348 THEN 26 WHEN 127017 THEN 27 WHEN 126764 THEN 28 WHEN 126637 THEN 29 WHEN 122170 THEN 30 WHEN 125630 THEN 31 WHEN 125515 THEN 32 WHEN 124487 THEN 33 WHEN 124374 THEN 34 WHEN 124343 THEN 35 WHEN 124327 THEN 36 WHEN 126838 THEN 37 WHEN 126720 THEN 38 WHEN 126718 THEN 39 WHEN 123510 THEN 40 WHEN 122966 THEN 41 WHEN 124385 THEN 42 WHEN 122754 THEN 43 WHEN 124312 THEN 44 WHEN 122311 THEN 45 WHEN 121577 THEN 46 WHEN 121028 THEN 47 WHEN 123354 THEN 48 WHEN 121682 THEN 49 WHEN 120993 THEN 50 WHEN 120860 THEN 51 WHEN 120750 THEN 52 WHEN 120460 THEN 53 WHEN 120459 THEN 54 WHEN 120397 THEN 55 WHEN 119178 THEN 56 WHEN 111723 THEN 57 WHEN 127644 THEN 58 WHEN 127610 THEN 59 WHEN 127609 THEN 60 ELSE order END WHERE NR_DMD IN(127585,124376,124163,127019,123816,124348,127017,126764,126637,122170,125630,125515,124487,124374,124343,124327,126838,126720,126718,123510,122966,124385,122754,124312,122311,121577,121028,123354,121682,120993,120860,120750,120460,120459,120397,119178,111723,127644,127610,127609)
22/01/2014 14:03:25 UPDATE `edited_table_name` SET `order` = CASE NR_DMD WHEN 127638 THEN 19 WHEN 127592 THEN 20 WHEN 124376 THEN 21 WHEN 124163 THEN 22 WHEN 127019 THEN 23 WHEN 123816 THEN 24 WHEN 124348 THEN 25 WHEN 127017 THEN 26 WHEN 126764 THEN 27 WHEN 126637 THEN 28 WHEN 122170 THEN 29 WHEN 125630 THEN 30 WHEN 125515 THEN 31 WHEN 124487 THEN 32 WHEN 124374 THEN 33 WHEN 124343 THEN 34 WHEN 124327 THEN 35 WHEN 126838 THEN 36 WHEN 126720 THEN 37 WHEN 126718 THEN 38 WHEN 123510 THEN 39 WHEN 122966 THEN 40 WHEN 124385 THEN 41 WHEN 122754 THEN 42 WHEN 124312 THEN 43 WHEN 122311 THEN 44 WHEN 121577 THEN 45 WHEN 121028 THEN 46 WHEN 123354 THEN 47 WHEN 121682 THEN 48 WHEN 120993 THEN 49 WHEN 120860 THEN 50 WHEN 120750 THEN 51 WHEN 120460 THEN 52 WHEN 120459 THEN 53 WHEN 120397 THEN 54 WHEN 119178 THEN 55 WHEN 111723 THEN 56 WHEN 127644 THEN 57 ELSE order END WHERE NR_DMD IN(127638,127592,124376,124163,127019,123816,124348,127017,126764,126637,122170,125630,125515,124487,124374,124343,124327,126838,126720,126718,123510,122966,124385,122754,124312,122311,121577,121028,123354,121682,120993,120860,120750,120460,120459,120397,119178,111723,127644)
I thought MySQL would deal with the simultaneous hits on the same table. But it looks like not. Perhaps it's some issue with table locks.
If you can give me any advice in any configuration or technique or even another better way to persist the reordered list, I'd be very greateful.
Thanks,
You could try to use the following session parameters before your update statement in your servlet.
"SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED"
That would allow that if two statements occurs almost instantanously, it would take the latest one even if the other process had not commited it to the server.
For sure it is better to send changes only if the end-user did not did any drag & drop for more than X seconds (ie: 2) and send only 1 request. That would solve the problem also.
Would you consider using something in your client side code so that you don't submit multiple AJAX requests simultaneously such as setTimeout when the list is reordered.
Perhaps something like this:
http://benalman.com/code/projects/jquery-message-queuing/examples/ajax/
I would suggest that you maintain a check for a period of inactivity and when there is a good amount of inactivity, send latest update (that has the cumulative update) to the server.
During the execution of your update query InnoDB locks every row that it traverses to find the rows it is modifying. So to reduce the number of locks you can split your query into two separate queries:
Get PRIMARY_KEY_LIST, suppose that the primary key of edited_table_name is id
SELECT id FROM edited_table_name WHERE NR_DMD IN(...)
Update basing on primary key list
UPDATE edited_table_name SET order = CASE... WHERE id IN (PRIMARY_KEY_LIST)
This will reduce the number of locks because it reduces the number of rows that the engine has to traverse.
Another option is to put an index on NR_DMD columns
I might have a handler to throttle the hits that come to the servlet probably in this scenario :
Rather than instantly applying every new order request that hits the servlet , I would store the info/details in a table say new_order_requests_table .
I would have a flag which is used/updated by this handler accordingly with status of itself , say : Idle , Under Progress , etc .
Whenever the above flag shows as Idle , I would consider to apply the latest row from the new_order_requests_table and probably delete all earlier rows from it .
For multiple users , you would need to modify the above accordingly .
Further , when it is needed to update many rows in a MySQL table in a single statement , sometimes it would be better with MyISAM which does table level locking as opposed to InnoDB which does row level locking . This is because InnoDB might be consuming considerable duration just to lock and unlock those many rows .You could try switching between MyISAM and InnoDB to find the better suitable engine for this table in your application .
I think you can do the following:
CREATE TEMPORARY TABLE Table1 (
NR_DMD int NOT NULL);
INSERT INTO Table1 (NR_DMD)
VALUES
(92059),(124376),(124163),(127019),(123816),(124348),(127017),(126764),(126637),
(122170),(125630),(125515),(124487),(124374),(124343),(124327),(126838),(126720),
(126718),(123510),(122966),(124385),(122754),(124312),(122311),(121577),(121028),
(123354),(121682),(120993),(120860),(120750),(120460),(120459),(120397),(119178),
(111723),(127644),(127610),(127609),(127542),(125799);
select * from Table1
22/01/2014 14:03:25
UPDATE `edited_table_name
` SET `order` = CASE NR_DMD WHEN 92059 THEN 22 WHEN 124376 THEN 23 WHEN 124163 THEN 24
WHEN 127019 THEN 25 WHEN 123816 THEN 26 WHEN 124348 THEN 27 WHEN 127017 THEN 28
WHEN 126764 THEN 29 WHEN 126637 THEN 30 WHEN 122170 THEN 31
WHEN 125630 THEN 32 WHEN 125515 THEN 33 WHEN 124487 THEN 34
WHEN 124374 THEN 35 WHEN 124343 THEN 36 WHEN 124327 THEN 37
WHEN 126838 THEN 38 WHEN 126720 THEN 39 WHEN 126718 THEN 40
WHEN 123510 THEN 41 WHEN 122966 THEN 42 WHEN 124385 THEN 43
WHEN 122754 THEN 44 WHEN 124312 THEN 45 WHEN 122311 THEN 46
WHEN 121577 THEN 47 WHEN 121028 THEN 48 WHEN 123354 THEN 49
WHEN 121682 THEN 50 WHEN 120993 THEN 51 WHEN 120860 THEN 52
WHEN 120750 THEN 53 WHEN 120460 THEN 54 WHEN 120459 THEN 55
WHEN 120397 THEN 56 WHEN 119178 THEN 57 WHEN 111723 THEN 58
WHEN 127644 THEN 59 WHEN 127610 THEN 60 WHEN 127609 THEN 61
WHEN 127542 THEN 62 WHEN 125799 THEN 63 ELSE order END
WHERE NR_DMD IN(Select NR_DMD from Table1);
Summary:
1)Create temp table and insert the values for first update.
2)
UPDATE tablename SET ColName =CASE STATEMENT
Where NR_DMD in ( SELECT NR_DMD FROM temp table)
3) Do the same for 2 other update statement.
I think this will help you to better perform.

Get the hits of those days which have more hits than previous day

How to Get the hits of those days which have more hits than previous day.
Table structure
Date Hits
1-Mar-2013 36
2-Mar-2013 2
3-Mar-2013 99
4-Mar-2013 82
5-Mar-2013 34
6-Mar-2013 36
7-Mar-2013 56
8-Mar-2013 81
9-Mar-2013 8
10-Mar-2013 99
11-Mar-2013 12
12-Mar-2013 76
13-Mar-2013 75
14-Mar-2013 80
15-Mar-2013 69
16-Mar-2013 12
17-Mar-2013 3
18-Mar-2013 75
19-Mar-2013 7
20-Mar-2013 54
21-Mar-2013 82
22-Mar-2013 50
23-Mar-2013 29
24-Mar-2013 17
25-Mar-2013 78
26-Mar-2013 97
27-Mar-2013 76
28-Mar-2013 57
29-Mar-2013 28
30-Mar-2013 17
Can somebody suggest me a mysql queryy for doing this in one query itself. The Date column tells the date and the hits colum gives the hits on that day.
Try something like this:
SELECT t2.*, t1.hits AS Previous_day_hits
FROM tab1 t1
INNER JOIN tab1 t2 ON t2.DATE = date_add(t1.DATE, INTERVAL 1 DAY)
AND t2.hits > t1.hits;
sqlfiddle demo
I added the column with Previous_day_hits for easier validation.
It's called a self join. Basically you get two copies of the table and work out a rule to link them

dlookup function in access

I'm first time trying to use dlookup function in access as below but I get blank output.
select dlookup ("quantity","test","series > 2000") from test
This is test table
id series quantity
1 1000 25
2 2000 33
3 3000 44
4 4000 55
5 5000 66
6 6000 77
I thought the above query will display all records from the table below which has series more than 2000 i.e. as below but it displays blank result.
id series quantity
3 3000 44
4 4000 55
5 5000 66
6 6000 77
I'm not sure if my syntax is incorrect or anything else. I already double checked my syntax from various sources though.
DLookup() returns a single value, which is not what I think you want. Just put your selection constraint in the WHERE clause.
SELECT id, series, quantity
FROM test
WHERE series > 2000;

Querying a table to get values based on no of digits of a parameter?

Considering the following table
I have a large table from which I can query to get the following table
type no of times type occurs
101 450
102 562
103 245
111 25
112 28
113 21
Now suppose I wanted to get a table which shows me the sum of no of times type occurs
for type starting with 1 then starting with 10,11,12,13.......19 then starting with 2, 20,21, 22, 23...29 and so on.
Something like this
1 1331 10 1257
11 74
12 ..
13 ..
.. ..
2 ... 20 ..
21 ..
Hope I am clear
Thanks
You really have two different queries:
SELECT [type]\100 AS TypePart, Count(t.type) AS CountOftype
FROM t
GROUP BY [type]\100;
And:
SELECT [type]\100 AS TypePart, [type] Mod 100 AS TypeEnd,
Count(t.type) AS CountOftype
FROM t
GROUP BY [type]\100, [type] Mod 100;
Where t is the name of the table.
Here on the first query i am getting something like this
utypPart CountOftype
1 29
2 42
3 46
4 50
5 26
6 45
7 33
9 1
it is giving me how many utyp are starting with 1 2 and so on
but whai i want is the sum of no of times those types occur for the utyp .