English 中文(简体)
从数据库中找到新闻项目档案途径的最佳办法是什么?
原标题:What is the best solution to get file paths for a news item from the database?

我有两个表格:newsnews_files。 表格包含newsId和其他一些栏目news_files。 www.un.org/spanish/ga/president 关于<代码>新闻记录,你至少可拥有5份档案。

从<代码>news中检索所有栏目和该栏所有文档的最佳办法是什么?

迄今为止,我有以下问询,但就每一条而言,www.code>news_files,则将同样的news记录(每个条目filePath与同一news

select n.* from news n 
left outer join news_files nf on n.Id = nf.newsid 
where n.Id = 1
最佳回答

For a one to many relationship like news to news_files, in order to get them all into one query you will, as you have found, need to retrieve duplicate column values from the "one" for each related row in the "many" table.

然而,我质疑是否需要在单一问题上这样做。 只有一个到一个关系,如果只有一个<条码>,就会有: 《<>新闻>/代码>浏览,试图在一栏中生产这些字眼,是有意义的。 但是,对于一位到许多人来说,你或许也只是问一下主要表格,然后问一下“many”关系表的相关段落。

/* Returns duplicate column values for each news_files row */
/* and all columns from both tables */
SELECT
  n.*,
  nf.*
FROM 
  news n
  LEFT OUTER JOIN news_files nf ON n.Id = nf.newsid
WHERE n.Id = 1

您可检索one栏,news_files,作为使用GROUP_CONCAT()的压缩名单,但该数字与仅仅检索所有浏览量完全相同。 虽然MySQL允许在<代码>中不出现一栏。 按<<<>代码/代码>列出的组别,如/code>,则其他RDBMS t,即为可携带性,GROUP_CONCAT()被置于一个子类中,以便与主表合并并检索所有栏目。

/* Comma-separated list of filePath and all cols from news */
SELECT 
  n.*,
  nf.files
FROM
  news 
  LEFT OUTER JOIN (
    SELECT newsid, GROUP_CONCAT(filePath) AS files FROM news_feeds
    GROUP BY newsid
  ) nf ON n.Id = nf.newsid
问题回答

假设你有两栏新闻,两栏新闻——......一栏,但不需要。

select n.Id, n.someothercolumn, null as newsId, null as filepath
from news n
where Id = 1

union

select null, null, nf.newsId, nf.filepath
from new_files nf
where newedId = 1

order by isnull(Id, newsId)

如果你们需要一些迅速和 d脏的工作,那么我们不是最喜欢的,而是应该工作!





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签