Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何使用SQLite根据相关的表值从一组表中提取数据?_Mysql_Sqlite - Fatal编程技术网

Mysql 如何使用SQLite根据相关的表值从一组表中提取数据?

Mysql 如何使用SQLite根据相关的表值从一组表中提取数据?,mysql,sqlite,Mysql,Sqlite,我有一套桌子: 1) **nanoInst**: rid | api_id | name | instLoc | instText | pixelPitch | logo | listImg 2) **nanoFiles**: rid | api_id | name | fileType | fileLoc 3) **nanoInd**: rid | api_id | name 4) **nanoRelInd**: rid | inst_id | ind_id 5) **nanoGa

我有一套桌子:

1) **nanoInst**:
rid | api_id | name | instLoc | instText | pixelPitch | logo | listImg

2) **nanoFiles**:
rid | api_id | name | fileType | fileLoc

3) **nanoInd**: 
rid | api_id | name

4) **nanoRelInd**: 
rid | inst_id | ind_id

5) **nanoGalleries**: 
rid | inst_id | file_id | gallery_order
我最初的SQL语句简单地从nanoInst表中选择了id和名称,从nanoFiles中选择了fileLoc,从nanoInst存储了nanoInst文件id的nanoInd中选择了名称,并在nanoInst和nanoInd与其id之间建立了关系

SELECT 
nanoInst.rid as instID, 
nanoInst.name AS nanoName, 
nanoFiles.fileLoc AS nanoFile, 
nanoInd.name AS indName 
FROM nanoInst 
LEFT JOIN nanoFiles ON nanoInst.logo = nanoFiles.rid 
LEFT JOIN nanoRelInd ON nanoInst.rid = nanoRelInd.inst_id 
LEFT JOIN nanoInd ON nanoRelInd.ind_id = nanoInd.rid
我需要调整此语句,以便仅在nanoFiles.fileType为“video”的情况下获得结果。我尝试简单地添加nanoFiles.fileType='video'的位置,但返回了0行,但我知道存在一些行

--编辑--

我发现,如果我可以计算该行包含视频的项目数,则运行WHERE子句进行筛选:

SELECT 
  nanoInst.rid as instID, 
  nanoInst.name AS nanoName, 
  nanoFiles.fileLoc AS nanoFile, 
  nanoInd.name AS indName, 
  nanoFiles.fileType as nanoType, 
  (SELECT
  COUNT(nanoGalleries.rid)
  FROM nanoGalleries
  LEFT JOIN nanoFiles ON nanoFiles.rid = nanoGalleries.file_id
  LEFT JOIN nanoInst ON nanoInst.rid = nanoGalleries.inst_id
  WHERE nanoFiles.fileType = 'video') AS vidCount
FROM 
  nanoInst 
  LEFT JOIN nanoFiles ON nanoInst.logo = nanoFiles.rid 
  LEFT JOIN nanoRelInd ON nanoInst.rid = nanoRelInd.inst_id 
  LEFT JOIN nanoInd ON nanoRelInd.ind_id = nanoInd.rid
WHERE vidCount > 0

问题是vidCount返回的视频总数不仅仅是该行的计数/nanoInst.rid

由于nanoRelInd或nanoInd中没有匹配的记录,记录可能会被过滤掉。如果是这样的话,这应该是可行的

SELECT 
  nanoInst.rid as instID, 
  nanoInst.name AS nanoName, 
  nanoFiles.fileLoc AS nanoFile, 
  nanoInd.name AS indName 
FROM 
  nanoInst 
  LEFT JOIN nanoFiles ON nanoInst.logo = nanoFiles.rid 
  LEFT outer JOIN nanoRelInd ON nanoInst.rid = nanoRelInd.inst_id 
  LEFT outer JOIN nanoInd ON nanoRelInd.ind_id = nanoInd.rid
WHERE 
  nanoFiles.fileType='video'

根据您原始帖子中的评论和您提供的sqlfiddle:

   SELECT * 
     FROM nanogalleries
LEFT JOIN nanofiles on(nanofiles.rid=nanogalleries.file_id)
    WHERE nanofiles.filetype='video';
根据评论更新答案

   SELECT nanogalleries.inst_id,
          nanoinst.name                AS nanoName,
          nanoFiles.fileLoc,
          nanoInd.name                 AS indName,
          count(nanogalleries.inst_id) AS vidCount
     FROM nanogalleries
LEFT JOIN nanofiles  ON nanofiles.rid = nanogalleries.file_id
LEFT JOIN nanoinst   ON  nanoinst.rid = nanogalleries.inst_id
LEFT JOIN nanoRelInd ON  nanoinst.rid =    nanoRelInd.inst_id
LEFT JOIN nanoInd    ON   nanoind.rid =    nanorelind.ind_id
    WHERE nanofiles.filetype = 'video'
 GROUP BY nanogalleries.inst_id;

如果在没有where子句的情况下运行查询,会得到多少行?将nanoFiles.fileType添加到选择列表并按nanoFiles.fileType desc排序,然后运行查询并在该列中查找“视频”值。您还可以在sqlfiddle.com上设置一个fiddle,使我们更容易帮助您。如果没有where子句,我将获得所有预期的行。您将nanoFiles.fileType添加到select中,您可以看到该列中确实有值为“video”的行?我问这个问题的原因是,如果您添加'Where nanoFiles.fileType='video',而您没有得到任何行,这意味着实际上没有任何行具有该列的值…当我添加该选择时,我只得到图像。。。我知道也有一些与视频相关的内容。我需要回顾我的陈述…我的猜测是源数据不是你认为的那样。。。您正在使用phpmyadmin、heidisql或其他工具吗?是的,但这些结果缺少nanoInst.rid、nanoInst.name、nanoFiles.fileLoc、nanoInd.name。我的结果显示所需项目所需的列。如果您查看此处的语句:您可以看到vidCount列。现在它显示所有视频5,但我正在尝试显示该行的视频数量。在这个例子中,它们都是0,除了rid 3有1,6有1,8有2基于你在sqlfiddle中所做的,我想我理解你想做什么,使用一组人可以更容易地完成