获取我使用UNION的mysql查询时出错

获取我使用UNION的mysql查询时出错,mysql,Mysql,我的SQL查询 SELECT DISTINCT a.*,categories_2625729.title AS category_title,categories_2625729.id AS category,c.id as cid, c.title, '' as `introtext`, 'com_hpj_content.ad' as content_type, '' as banner, '' as `fulltext`, '' as sheettext, c.publish_up, c.p

我的SQL查询

SELECT DISTINCT a.*,categories_2625729.title AS category_title,categories_2625729.id AS category,c.id as cid, c.title, '' as `introtext`, 'com_hpj_content.ad' as content_type, '' as banner, '' as `fulltext`, '' as sheettext, c.publish_up, c.publish_down, c.price, c.advertiser_id, c.seller_id, c.sold, c.soldupdated,0 AS is_favorite
FROM `jos_hpj_content_item` AS a
LEFT JOIN jos_categories AS categories_2625729 ON categories_2625729.id = a.category
LEFT JOIN jos_hpj_content_item_ad AS cff ON cff.id = a.ref_id AND a.content_type = 'com_hpj_content.ad'
LEFT JOIN jos_fields_values AS fields_values_min_maxyear ON fields_values_min_maxyear.item_id = a.ref_id AND fields_values_min_maxyear.field_id=8
LEFT JOIN jos_fields_values AS fields_values_min_max_2mileage ON fields_values_min_max_2mileage.item_id = a.ref_id AND fields_values_min_max_2mileage.field_id=10
LEFT JOIN jos_hpj_content_item_ad c ON c.`id` = a.`ref_id` AND a.`content_type`='com_hpj_content.ad'
WHERE a.state = 1 AND (`c`.title LIKE '%22%' OR c.`introtext` LIKE '%22%' OR a.synonym LIKE '%22%') AND a.content_type IN ('com_hpj_content.ad') AND a.ref_id IN (53,307,353,354,572,644,717,934,978,1056,1086,1128,1149,1199,1276,1294,1314,1324,1347,1351,1396,1411,1462,1470,1513,1537,1740,1741,1836,1861,1916,1988,1989,2005,2006,2124,2158,2266,2267,2268,2277,2511,2575,2577,2582,2585,2587,2654,2815,2894,2906,2975,3021,3179,3194,3219,3224,3239,0) AND a.state = 1
UNION (SELECT name FROM jos_users WHERE name LIKE '%22%')
我犯了个错误

#1222 - The used SELECT statements have a different number of columns
如果删除UNION,则返回sql查询结果,如果保留UNION,则返回错误
我试图从jos_user中搜索c.title或name中不同表格中的数字,并返回结果,正如@danblack在评论中指出的那样,错误是

#1222 - The used SELECT statements have a different number of columns
表示两个
SELECT
语句上的列数不同。在

选择一个不同的字符。*,
类别?2625729。标题作为类别?标题,
类别U2625729.id作为类别,c.id作为cid,
c、 头衔,
“”作为“introtext”,
“com_hpj_content.ad”作为内容类型,
""作为旗帜,,
“”作为“全文”,
“”作为文本,
c、 发表,,
c、 把你放下,
c、 价格,
c、 广告商身份证,
c、 卖方编号:,
c、 售出,
c、 soldupdated,0为您的最爱
从…起
…您选择的列比此列多得多

选择名称
从…起
这两个
SQL
语句的列数必须匹配,以便
UNION
能够将第二个查询的结果添加到第一个查询的结果中

因此,这将起作用:

选择不同的c.title
从…起
协会
选择名称
从…起

因为现在两个
SELECT
语句只返回一列。

如果您阅读了错误消息,您认为这意味着什么?你认为结果会是什么样子?@danblack我不知道这是什么意思,我猜。有两个SELECT语句。你认为它在谈论什么专栏?如果仍然卡住,回答第二个问题,并回顾实际操作。@danblack确定这是关于第二次选择,但我不理解错误;我正在尝试在c.title和user中查找22。但是我需要在我的select@pysql显然,这不是真的。@pysql可能是这样。但是你不能像你尝试的那样使用
UNION
。如果您需要在结果集中增加一列,那么
UNION
不是正确的方法。如果结果集中需要其他行,则需要构造第二个
SELECT
以进行匹配。除了UNION之外,还有其他方法可以在多个表中搜索吗?可以执行多个SELECT语句。这样,您就不需要阅读/理解/搜索此错误,也不需要解决下一个问题,即使大量的
user.name
行看起来像
a.*,。
。这里要学习的SQL的基本原理是SQL返回行,所有行的列数都相同。