![](/img/trans.png)
[英]SQL subquery to return MIN of a column and corresponding values from another column
[英]Return list of values from SQL Subquery inside a View
我在這里有四個表(研究,出版物,位置,葯物)和由葯物和研究之間的多對多關系引起的聯接表。
我用以下腳本創建了一個視圖:
SELECT
`l`.`country` AS `country`,
`l`.`latitude` AS `latitude`,
`l`.`longitude` AS `longitude`,
`l`.`region` AS `region`,
`s`.`enrolment` AS `enrolment`,
`s`.`diagnosis` AS `diagnosis`,
`s`.`registry_ID` AS `registry_ID`,
`s`.`status` AS `status`,
`s`.`site_name` AS `site_name`,
`s`.`study_no` AS `study_no`,
`p`.`pub_year` AS `pub_year`,
`p`.`URL` AS `url`,
`p`.`authors` AS `authors`,
`p`.`pubMedId` AS `pubMedId`,
`p`.`pub_title` AS `pub_title`,
`d`.`name` AS `name`
FROM
(((`VL`.`Location` `l`
join `VL`.`Study` `s`)
join `VL`.`Publication` `p`)
join `VL`.`Drugs` `d`)
WHERE
((`l`.`id` = `s`.`location_id`)
and (`p`.`study_id` = `s`.`id`)
and `d`.`id` in ((SELECT
`VL`.`Study_Drugs`.`drugses_id`
FROM
`VL`.`Study_Drugs`
WHERE
(`VL`.`Study_Drugs`.`studiesList_id` = `s`.`id`))))
現在,我需要的是子查詢為我提供每個研究中可以找到的葯物的完整列表,這就是我單獨執行此查詢時會發生的情況:
SELECT d.name
FROM VL.Drugs d
WHERE d.id IN(SELECT drugses_id
FROM Study_Drugs
WHERE studiesList_id = 'whatever id')
所以,問題是為什么它單獨返回查詢的完整列表,而不是在View的腳本中使用的同一查詢中?
EDIT
以包含樣本數據和希望的結果:
研究:
'2', 'Spleen', '70', NULL, 'KALA-AZAR MEDICAL RESEARCH CENTER, RAMBAG ROAD , MUZAFFARPUR, BIHAR -842001, INDIA ', 'Published', '75', NULL, '68'
出版物:
'12', 'http://www.ncbi.nlm.nih.gov/pubmed/10897369', 'Sundar S, Gupta LB, Rastogi V, Agrawal G, Murray HW.', '10897369', 'Short-course, cost-effective drugs with amphotericin B-fat emulsion cures visceral leishmaniasis.', '2000', '2'
位置:
'68', 'India', '26.118589', '85.400719', 'India Subcontinent'
葯物(單獨執行子查詢時):
'Pentavalent Antimonial'
'Miltefosine'
因此,除了研究和出版物中的一些不必要的字段之外,我希望從我的視圖中獲得所有這些信息。 最終值(葯物)由與研究ID相關的所有葯物組成,我現在每行得到的內容如下:
'India', '26.118589', '85.400719', 'India Subcontinent', '70', 'Spleen', NULL, 'Published', 'KALA-AZAR MEDICAL RESEARCH CENTER, RAMBAG ROAD , MUZAFFARPUR, BIHAR -842001, INDIA ', '75', '2000', 'http://www.ncbi.nlm.nih.gov/pubmed/10897369', 'Sundar S, Gupta LB, Rastogi V, Agrawal G, Murray HW.', '10897369', 'Short-course, cost-effective drugs with amphotericin B-fat emulsion cures visceral leishmaniasis.', 'Pentavalent Antimonial'
如您所見,最后一個值僅由列表中的第一種葯物形成
我想你希望你的觀點看起來像這樣
create view your_view
as
select s.*, d.* --explicit name that stuff
from Location l
inner join Study s on l.id = s.location_id
inner join Study_Drugs sd on s.id = sd.study_id
inner join Drugs d on d.id = sd.drugs_id
inner join Publications p on s.id = p.study_id;
select * from your_view where study_id = 1; -- whatever id
我不確定你想要的出版物是什么。 請使用帶有“內部聯接”的“新”符號,而不是您使用的“where”-clause。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.