How to sort id's widt dot's. I have this table:
1, 1.2, 2, 3, 3.1, 3.2, 4, 5, 100, 101, 200 ...
If we use SELECT * FROM table ORDER BY id ASC it will show:
1, 100, 101, 1.2, 2, 200, 3, 3.1, 3.2, 4, 5 ...
But I need this:
1, 1.2, 2, 3, 3.1, 3.2, 4 ,5, 100, 101, 200 ...
Try this::
SELECT * FROM table ORDER BY CAST(id AS DECIMAL) ASC
other things you can do is make your column in database as FLOAT
and then you order your numbers as you do in your sql without CAST
SELECT * FROM table ORDER BY id ASC
and it will work
Related
I would like to remove the number 6 from the column access (type json) without knowing its array location.
Example access row 1: [1, 2, 3, 4, 5, 6, 7, 8, 9]
CREATE TABLE example (
id int NOT NULL AUTO_INCREMENT,
access json NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO `example` (`access`) VALUES ('[1, 2, 3, 4, 5, 10, 20, 30]');
SELECT * FROM `example`;
SELECT JSON_SEARCH(`access`, 'one', '30') from `example`;
The JSON_SEARCH doesn't return any results.
You've probably moved on but should anyone else find themselves in the same position as I, trying to search arrays of integers... JSON_SEARCH doesn't support the function.
https://bugs.mysql.com/bug.php?id=79233
I have a form which allows user to choose a group of products and compare the prices across different suppliers
This is the data looks like:
Suppliers table
Suppliers (id, name)
Values (1, 'Shop 1')
Values (2, 'Shop 2')
Values (3, 'Shop 3')
...
Devices table
Devices (id, name)
Values (1, 'IBM x201')
Values (2, 'IBM x220')
Values (3, 'IBM x230')
...
Products table
Products (id, device_id, supplier_id, hdd_size, cpu, price)
Values (1, 1, 1, 160, 2.0, 300)
Values (2, 1, 1, 320, 2.0, 330)
Values (3, 1, 1, 160, 2.2, 360)
Values (4, 1, 1, 320, 2.2, 410)
Values (5, 1, 2, 160, 2.0, 310)
Values (6, 1, 2, 320, 2.0, 340)
Values (7, 1, 2, 160, 2.2, 370)
Values (8, 1, 2, 320, 2.2, 420)
...
So user might request prices for q or more devices giving different crytria:
Request:
[
{ device_id: 1, hdd: 160 },
{ device_id: 3, cpu: 2.0}
]
So the user wants to buy IBM x201 with hdd of at least 160GB and IBM x230 with cpu of at least 2.0GHZ.
This should return prices grouped by a supplier_id, for example
{ supplier_id: 1, price: 730, products: [
{ id: 1, hdd_size: 160, cpu: 2.0, price: 300 },
{ id: 22, hdd_size: 160, cpu: 2.2, price: 430 },
]},
{ supplier_id: 1, price: 780, products: [
{ id: 1, hdd_size: 160, cpu: 2.0, price: 300 },
{ id: 37, hdd_size: 320, cpu: 2.2, price: 480 },
]},
{ supplier_id: 2, price: 750, products: [
{ id: 12, hdd_size: 160, cpu: 2.0, price: 310 },
{ id: 39, hdd_size: 160, cpu: 2.0, price: 440 },
]},
...
How to do it in SQL. I'm trying different ways but I'm getting to a point where I think it might be actually easier to do the grouping and calculations outside of DB.
Any thoughts?
EDIT:
http://sqlfiddle.com/#!2/162b62
Assuming you need the 2 lowest valued products satisfying the conditions for each supplier, following is the solution
select supplier_id, sum(price)as sum, array[
array[(select id from products where supplier_id=P.supplier_id order by price asc limit 1),
(select hdd_size from products where supplier_id=P.supplier_id order by price asc limit 1),
(select cpu from products where supplier_id=P.supplier_id order by price asc limit 1),
(select price from products where supplier_id=P.supplier_id order by price asc limit 1)] ,
array[(select id from products where supplier_id=P.supplier_id order by price asc offset 1 limit 1),
(select hdd_size from products where supplier_id=P.supplier_id order by price asc offset 1 limit 1),
(select cpu from products where supplier_id=P.supplier_id order by price asc offset 1 limit 1),
(select price from products where supplier_id=P.supplier_id order by price asc offset 1 limit 1)]
]
from products as P where device_id=(select id from devices where name ilike 'ibm x201') and cpu>=2.0 and hdd_size>= 160
group by supplier_id having count(id)>2 ;
You can easily modify this to get multiple counts of products by creating the sql statement in a for loop in the language you are intending to use it in. It is lengthy and I really don't know about the performance, but it does the job.
It does fare well than getting the count for all the suppliers individually (tested for 2 suppliers). I am not sure how it will fare when the tuple count increases.
Try
SELECT
name, device_id, supplier_id, hdd, cpu, price
FROM
products p JOIN
devices d ON p.device_id = d.id
WHERE
(name = 'ibm x201' AND hdd >= 160)
OR
(name = 'ibm x230' AND cpu >= 2.0)
SQLFIDDLE
Split the data selectively
I have a table with following fields
Table-1
Publication_ID, Student_ID, Q1,A1,Q2,A2,Q3,A3,Q4,A4......................Q249,A249,Q250,A250
Sample data
100, 123, 1, B, 2, A, 3, C,4, B, 5, D, 6, B,..........................120, C
100, 124, 1, C, 2, C, 3, D,4, C, 5, D, 6, B,.....................109, B
100, 125, 1, B, 2, C, 3, C,4, B, 5, D, 6, A,..........................120, C
100, 126, 1, C, 2, B, 3, A,4, B, 5, C, 6, D,..............................................250, D
Now I would like to save the data od above table to Another table in this format
Table-2
Publication_ID, Student_ID, Q,A
100, 123, 1,B
100, 123, 2,A
100, 123, 3,C
100, 123, 4,B
100, 123, 5,D
100, 123, 6,B
100, 124, 2,A
100, 124, 3,C
100, 124, 4,B
100, 124, 5,D
100, 124, 6,B
100, 125, 1,B
100, 125, 2,A
100, 125, 3,C
100, 125, 4,B
100, 125, 5,D
100, 125, 6,B
Please note that the number of field filled in the table 1 may not be same for all the rows of data. So it should not insert empty fields to the Table-2 from Table-1
Please help me in this regard
INSERT INTO table-2(SELECT Publication_ID,Student_ID,Q1,Q2,....A1,A2... FROM table-1)
If i understood that correctly
I assume you are trying to normalize some kind of table. So it shouldn't be an issue to perform multiple data manipulation statements in order to achieve the desired result. Something like that might do the trick:
SET autocommit = 0
-- repeat for n for 1 to 249
INSERT INTO T2
SELECT Publication_ID, Student_ID, Qn, An WHERE Qn IS NOT NULL
COMMIT
If you really need to execute the migration without using a host language, you could use a procedure:
CREATE PROCEDURE migrate()
BEGIN
DELETE FROM T2;
SET #n = 1;
REPEAT
SET #p = CONCAT('INSERT INTO T2 SELECT Publication_ID, Student_ID, Q', #n, ' as Q A', #n, ' WHERE Q IS NOT NULL');
PREPARE stmt FROM #p;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET #n = #n+1;
UNTIL #n > 249
END REPEAT;
END //
CALL migrate()
Currently I am storing adjacencies in a php file in an array. Here's a sample of it:
$my_neighbor_lists = array(
1=> array(3351=> array (2, 3, 5 , 6, 10)),
2=> array(3264=> array (322, 12, 54 , 6, 10), 3471=>array (122, 233, 35 , 476, 210)),
3=> array(3309=> array (52, 32, 54 , 36, 210), 3469=>array (152, 32, 15 , 836, 10)),
etc
I would like to basically migrate this into a db. Any suggestions on how many table I should have? I am looking at three tables here.
two tables:
1. vertices (id)
2. edgecost (idfrom, idto, time, cost)
Table structure is like given below
id Name
1 Mukesh
2 Shiwam
3 deepak
4 ------
I want in resultant Like 1,2,3,4 it is possible?
Use GROUP_CONCAT():
SELECT GROUP_CONCAT(id)
FROM table
It's as simple as it gets. Cularis' answer specifies using the , separator, but it's not actually necessary, unless you want spaces between the commas:
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49
Which you didn't specify in your OP.
SELECT GROUP_CONCAT(id) FROM yTable
GROUP_CONCAT
Wth do you need this?