我有一个SQL数据库,其中包含一个名为'users'的表和一个名为industriesrepresented
在我的页面上,我有一个类似$myindustry = '97'
在我的数据库的字段中,用户从各个行业中进行选择,每个行业都有唯一的ID。 ie - 97,89,83,44,42
然后我要运行一个查询(SELECT * FROM
个用户)
但是我想按顺序排列并显示在“ industriesrepresented”列中输入了“ 97”的用户
并将它们放在列表的顶部。
希望这很有意义
答案 0 :(得分:0)
您可以使用#Create the forecasts vector to store the predictions
windowLength = 100
foreLength<-365
MSforecasts <- vector(length=foreLength)
for (d in 1:foreLength) {
# Obtain the rolling window
RollingWindow = window(FileAllData$Returns, start=(6850+d), end=(6850+windowLength+d))
MSmodel<-CreateSpec(variance.spec = list(model = c("sGARCH", "gjrGARCH", "eGARCH")),
distribution.spec = list(distribution = c("snorm", "std", "sged")),
switch.spec = list(do.mix = FALSE))
MSmodel_est<-FitML(spec = MSmodel, data=RollingWindow)
#Forecasting
MSforecast<-predict(MSmodel_est, nahead = 1)
MSforecasts[d]<-MSforecast$vol
}
MSforecasts
。
LIKE
该表达式将产生一个(隐式)布尔值,如果...
ORDER BY concat(',', industriesrepresented, ',') LIKE '%,97,%' DESC
列中为97,则该表达式为1
。由于0
> 1
,您必须使用0
。
答案 1 :(得分:0)
SELECT * FROM users ORDER BY find_in_set("'.$myindustry.'", industries) DESC
另一种解决方案
select *
from users
order by (case industries when "'.$myindustry.'" then 0 else 1 end)
答案 2 :(得分:0)
这不是应该使用数据库的方式。
首先,考虑一下users
和industries
之间的关系。
用户与行业之间的关系是(0,n)-(0,n)。通常称为多对多
您有2个表users
和industries
可以用这种方式表示:
用户
id | name
行业
id | name
在SQL中,要建立这种关系,您需要一个第三个表,其中包含两个表的外键
users_industries
user_id | industry_id
为了避免重复输入,第三张表的主键是两个FK的组成
模式(MySQL v5.7)
CREATE TABLE users
(
id INT(6) PRIMARY KEY NOT NULL AUTO_INCREMENT,
name VARCHAR(255)
);
CREATE TABLE industries
(
id INT(6) PRIMARY KEY NOT NULL AUTO_INCREMENT,
name VARCHAR(255)
);
CREATE TABLE users_industries
(
user_id INT(6) NOT NULL,
industry_id INT(6) NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (industry_id) REFERENCES industries(id),
PRIMARY KEY (user_id, industry_id)
);
INSERT INTO users VALUES (default, "John"), (default, "Jane"), (default, "Bob"), (default, "Mary");
INSERT INTO industries VALUES (default, "Renault"), (default, "Peugeot"), (default, "Citroen"), (default, "Venturi");
INSERT INTO users_industries VALUES (1, 1), (1, 3), (2, 1), (2, 2), (2, 3), (2, 4), (3, 2), (3, 3), (3, 4), (4, 4);
查询#1 :如何使所有用户具有各自的行业?
SELECT u.id AS "User id",
u.name AS "User name",
i.id AS "Industry id",
i.name AS "Industry name"
FROM users u
LEFT JOIN users_industries ui
ON u.id = ui.user_id
LEFT JOIN industries i
ON i.id = ui.industry_id
ORDER BY u.id;
输出
| User id | User name | Industry id | Industry name |
| ------- | --------- | ----------- | ------------- |
| 1 | John | 1 | Renault |
| 1 | John | 3 | Citroen |
| 2 | Jane | 1 | Renault |
| 2 | Jane | 2 | Peugeot |
| 2 | Jane | 3 | Citroen |
| 2 | Jane | 4 | Venturi |
| 3 | Bob | 2 | Peugeot |
| 3 | Bob | 3 | Citroen |
| 3 | Bob | 4 | Venturi |
| 4 | Mary | 4 | Venturi |
查询#2 :如何获取特定用户的行业名称?
SELECT i.name AS "Industry name"
FROM industries i
LEFT JOIN users_industries ui
ON i.id = ui.industry_id
WHERE ui.user_id = 3
ORDER BY i.id;
输出
| Industry name |
| ------------- |
| Peugeot |
| Citroen |
| Venturi |
查询#3 :如何获取特定行业的用户名?
SELECT u.name AS "User name"
FROM users u
LEFT JOIN users_industries ui
ON u.id = ui.user_id
WHERE ui.industry_id = 3
ORDER BY u.id;
输出
| User name |
| --------- |
| John |
| Jane |
| Bob |