[英]Illegal mix of collations for operation 'UNION' Nodejs Mysql
I´m trying to get all the posts that a user wrote using this sql:我正在尝试获取用户使用此 sql 编写的所有帖子:
'SELECT * FROM users WHERE username=? UNION ALL SELECT * FROM posts WHERE writer_name=?'
And i´m getting this error:我收到这个错误:
"Illegal mix of collations for operation 'UNION'"
tables桌子
CREATE TABLE `claudioBlog`.`users` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`username` VARCHAR(100) NOT NULL ,
`email` VARCHAR(100) NOT NULL ,
`age` VARCHAR(100),
`discription` VARCHAR (255),
`ocupation` VARCHAR (100),
`profile_image` VARCHAR (100)
`password` VARCHAR(255) NOT NULL ,
PRIMARY KEY (`id`), UNIQUE (`email`)
) ENGINE = InnoDB
CREATE TABLE posts (
id int(11) NOT NULL AUTO_INCREMENT,
title VARCHAR(50) NOT NULL,
category VARCHAR(50) NOT NULL,
body VARCHAR(2000) NOT NULL,
writer_id VARCHAR(50) NOT NULL,
writer_name VARCHAR(50) NOT NULL,
post_img VARCHAR(100),
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
What do i have to chage?我有什么要改变的?
You want a join
, not union
.你想要一个join
,而不是union
。 Presumably, writer_id
is a foreign key reference to users(id)
据推测, writer_id
是对users(id)
的外键引用
SELECT p.*
FROM users u JOIN
posts p
ON p.writer_id = u.id
WHERE u.username = ? ;
Note: If write_id
does indeed refer to users(id)
, it is inappropriate to store write_name
in posts
, unless -- for some strange reason -- it can vary by post and is not the users real name.注意:如果write_id
确实指的是users(id)
,则将write_name
存储在posts
是不合适的,除非——出于某种奇怪的原因——它可能因帖子而异,而不是用户的真实姓名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.