duankaolei2921 2012-02-27 03:49
浏览 441
已采纳

MYSQL查询具有不同字段的两个表

Here is my search query, which is putting out an argument error.
What do I need to do to correct it. Basically I need to put both of these tables together and pull out the next 5 events within the two tables by the start date. You will also notice that the last field in each table is different from one another.

$CEsql = mysql_query("
  SELECT id, title, start, end, allDay
  FROM calendar 
  WHERE mem_id='$logOptions_id' 
    AND start >= DATE_SUB( CURDATE( ) ,INTERVAL 0 MONTH ) 

  UNION ALL 

  SELECT id, title, start, end, allDay
  FROM team_calendar 
  WHERE team_id IN ($team_array) 
    AND start >= DATE_SUB( CURDATE( ) ,INTERVAL 0 MONTH ) 
  ORDER BY start ASC LIMIT 5");

I am using MYSQL 5.0 and PHP.

Update

mysql_error displays:

1267: Illegal mix of collations for operation 'UNION'

  • 写回答

2条回答 默认 最新

  • doujieyu7062 2012-02-27 07:52
    关注

    Run SHOW CREATE TABLE statements for both tables. Find differences between two tables, check charsets and collations as Leap Bun suggested. Then you may change field charset/collation, or just try to use COLLATE clause in your SELECT query. Something like this -

    SELECT id, title, start, end, allDay
      FROM calendar 
      WHERE mem_id='$logOptions_id' 
        AND start >= DATE_SUB( CURDATE( ) ,INTERVAL 0 MONTH ) 
    
      UNION ALL 
    
      SELECT id, 
        title COLLATE collation_name AS title, -- write your collation here
        start, end, allDay
      FROM team_calendar 
      WHERE team_id IN (1,2) 
        AND start >= DATE_SUB( CURDATE( ) ,INTERVAL 0 MONTH ) 
      ORDER BY start ASC LIMIT 5
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示