如何从此查询中计算和字段名称

时间:2016-10-05 08:33:20

标签: php mysql

我有这样的查询:

SELECT publisher, Count(publisher) 
FROM (SELECT NULL author, title, NULL journal, publisher, NULL abstracts, keyword, subject 
       FROM   dental) AS one 
WHERE ( author LIKE '%Bastone%' 
          OR title LIKE '%Bastone%' 
          OR journal LIKE '%Bastone%' 
          OR publisher LIKE '%Bastone%' 
          OR abstracts LIKE '%Bastone%' 
          OR keyword LIKE '%Bastone%' 
          OR subject LIKE '%Bastone%' ) 
UNION ALL 
SELECT * 
FROM  (SELECT author, title, journal, publisher, abstracts, keyword, NULL subject 
        FROM   wiley) AS one 
WHERE ( author LIKE '%Bastone%' 
          OR title LIKE '%Bastone%' 
          OR journal LIKE '%Bastone%' 
          OR publisher LIKE '%Bastone%' 
          OR abstracts LIKE '%Bastone%' 
          OR keyword LIKE '%Bastone%' 
          OR subject LIKE '%Bastone%' ) 
UNION ALL 
SELECT * 
FROM  (SELECT author, title, journal, publisher, abstracts, keyword, NULL subject 
        FROM   sage) AS one 
WHERE ( author LIKE '%Bastone%' 
          OR title LIKE '%Bastone%' 
          OR journal LIKE '%Bastone%' 
          OR publisher LIKE '%Bastone%' 
          OR abstracts LIKE '%Bastone%' 
          OR keyword LIKE '%Bastone%' 
          OR subject LIKE '%Bastone%' ) 
UNION ALL 
SELECT * 
FROM  (SELECT author, title, journal, publisher, abstracts, keyword, NULL subject 
        FROM   elsevier) AS one 
WHERE ( author LIKE '%Bastone%' 
          OR title LIKE '%Bastone%' 
          OR journal LIKE '%Bastone%' 
          OR publisher LIKE '%Bastone%' 
          OR abstracts LIKE '%Bastone%' 
          OR keyword LIKE '%Bastone%' 
          OR subject LIKE '%Bastone%' ) 
UNION ALL 
SELECT * 
FROM  (SELECT author, title, journal, publisher, abstracts, keyword, NULL subject 
        FROM   indianjournal) AS one 
WHERE ( author LIKE '%Bastone%' 
          OR title LIKE '%Bastone%' 
          OR journal LIKE '%Bastone%' 
          OR publisher LIKE '%Bastone%' 
          OR abstracts LIKE '%Bastone%' 
          OR keyword LIKE '%Bastone%' 
          OR subject LIKE '%Bastone%' ) 
GROUP  BY publisher 

但是当我执行它时,我从sql收到错误。所以请帮助我解决如何通过此查询获取计数和字段名称

错误我得到的是:

  

#1222 - 使用的SELECT语句具有不同的列数

2 个答案:

答案 0 :(得分:0)

您可以使用此查询(如果您愿意,但我认为在实际条件下,任何方式都会非常慢......)。 如果你想计算Publisher的rezult,从查询中删除COUNT(),并在PHP脚本中查询后,使用count()php函数,只计算返回结果的数组。

PSEUDO CODE:

$query = 'SELECT ... ';
$return_result = mysqli_query($query);

$count = count($return_result);

答案 1 :(得分:0)

你的陈述中的3个错误1) - 第一个选择与所有其他选择不同 - 我建议你使它相同2)在组之前需要一个右括号3)子查询需要一个别名 以下测试确定

SELECT Publisher,COUNT(Publisher) 
FROM
(
select * from (SELECT NULL author, Title, NULL journal, publisher, NULL abstracts, keyword, subject FROM dental) AS one 
WHERE (Author LIKE '%Bastone%' OR Title LIKE '%Bastone%' OR Journal LIKE '%Bastone%' OR Publisher LIKE '%Bastone%' 
OR abstracts LIKE '%Bastone%' OR Keyword LIKE '%Bastone%' OR Subject LIKE '%Bastone%') 
UNION ALL 
SELECT * FROM (SELECT author, title, journal, publisher, abstracts, keyword, NULL subject FROM wiley) AS one 
WHERE (Author LIKE '%Bastone%' OR Title LIKE '%Bastone%' OR Journal LIKE '%Bastone%' OR Publisher LIKE '%Bastone%' 
OR abstracts LIKE '%Bastone%' OR Keyword LIKE '%Bastone%' OR Subject LIKE '%Bastone%') 
UNION ALL 
SELECT * FROM (SELECT author, title, journal, publisher, abstracts, keyword, NULL subject FROM sage) AS one 
WHERE (Author LIKE '%Bastone%' OR Title LIKE '%Bastone%' OR Journal LIKE '%Bastone%' OR Publisher LIKE '%Bastone%' 
OR abstracts LIKE '%Bastone%' OR Keyword LIKE '%Bastone%' OR Subject LIKE '%Bastone%') 
UNION ALL 
SELECT * FROM (SELECT author, title, journal, publisher, abstracts, keyword, NULL subject FROM elsevier) AS one 
WHERE (Author LIKE '%Bastone%' OR Title LIKE '%Bastone%' OR Journal LIKE '%Bastone%' OR Publisher LIKE '%Bastone%' 
OR abstracts LIKE '%Bastone%' OR Keyword LIKE '%Bastone%' OR Subject LIKE '%Bastone%') 
UNION ALL 
SELECT * FROM (SELECT author, title, journal, publisher, abstracts, keyword, NULL subject FROM indianjournal) AS one 
WHERE (Author LIKE '%Bastone%' OR Title LIKE '%Bastone%' OR Journal LIKE '%Bastone%' OR Publisher LIKE '%Bastone%' 
OR abstracts LIKE '%Bastone%' OR Keyword LIKE '%Bastone%' OR Subject LIKE '%Bastone%') 
) s
GROUP BY Publisher