請問如果我需要做查表的動作
類似這樣圖中這樣,需要兩個參數來找出最終要的結果
原本想說把資料丟進資料庫,然後用語法來查,
但是我想不到該如何做出類似這種二維表的做法
還是說這種需求應該要用特殊的資料結構來存比較好?
求搜尋關鍵字或是SQL語法
感謝大家
=========== 補充 ==========
抱歉沒有說明清楚
1~1.9指的是需要當輸入值為1~1.9時查詢這個欄位(不是string)
並且跟row的50,60,70查詢到正確的數值
資料庫建表
create table tablename (
x1 float ,
x2 float ,
y float ,
value float
)
資料
insert into tablename (x1,x2,y,value) values (1,1.9,50,1356) , (2,2.9,50,1546) ,(3,3.9,50,1703) ,(4,4.9,50,1905) , (1,1.9,60,1768)
-- ................. 以下比較辦理
查詢
declare @A as float
set @A = 4.3
declare @B as float
set @B = 70
select value from tablename where (@A between x1 and x2) and (y = @B)
(更新,之前between用法有誤,改正)
簡單的DEMO
最標準的作法:多對多
-- 欄
CREATE TABLE col (
id INT NOT NULL AUTO_INCREMENT,
min_num FLOAT,
max_num FLOAT,
PRIMARY KEY (id)
);
INSERT INTO col
VALUES (null, 1, 1.9);
INSERT INTO col
VALUES (null, 2, 2.9);
INSERT INTO col
VALUES (null, 3, 3.9);
SELECT * FROM col;
-- 列
CREATE TABLE row (
id INT NOT NULL AUTO_INCREMENT,
value FLOAT,
PRIMARY KEY (id)
);
INSERT INTO row
VALUES (null, 50);
INSERT INTO row
VALUES (null, 60);
SELECT * FROM row;
-- 資料
CREATE TABLE data (
id INT NOT NULL AUTO_INCREMENT,
col_id INT NOT NULL,
row_id INT NOT NULL,
data VARCHAR(255),
PRIMARY KEY (id)
);
INSERT INTO data
VALUES (null, 1, 1, '1356');
INSERT INTO data
VALUES (null, 1, 2, '1768');
INSERT INTO data
VALUES (null, 1, 3, '1920');
INSERT INTO data
VALUES (null, 2, 1, '1546');
INSERT INTO data
VALUES (null, 2, 2, '1911');
INSERT INTO data
VALUES (null, 2, 3, '2034');
SELECT CONCAT(col.min_num, '~', col.max_num) as col_value, row.value as row_value, data.data
FROM col, row, data
WHERE col.id = data.col_id
AND row.id = data.row_id
-- 尋找欄=1~1.9、列=60
AND (1.3 BETWEEN col.min_num AND col.max_num)
AND row.value = 60
;