[英]How to count between two MySQL tables using MySQL and PHP?
For example, how can I count how many times the tag HTML is displayed in the a_id
column and then display all the tags that are related to the a_id
column in alphabetical order. 例如,我如何计算标签HTML在
a_id
列中显示多少次,然后按字母顺序显示与a_id
列相关的所有标签。
Here is the MySQL tables 这是MySQL表
CREATE TABLE a_tags (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
tag_id INT UNSIGNED NOT NULL,
a_id INT UNSIGNED NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE tags (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
tag VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);
Here is whats in the MySQL tables 这是MySQL表中的内容
TABLE tags
id tag
1 HTML
2 HTML
3 CSS
4 PHP
5 HTML
TABLE a_tags
id tag_id a_id
1 1 3
2 2 2
3 3 3
4 4 3
5 5 3
And Here is the code. 这是代码。
$result = mysql_query("SELECT a_tags.*, tags.*
FROM a_tags
INNER JOIN tags ON tags.id = a_tags.tag_id
WHERE a_tags.users_a_id=3
ORDER BY users_a_id DESC");
Woudln't 不会
SELECT * FROM `a_tags` WHERE a_id = (SELECT `id` FROM `tags` WHERE `tag` = 'HTML')
work? 工作?
Edit: This might not be what you want it to do. 编辑:这可能不是您想要的。 If you could clarify your question, it'd be helpful.
如果您可以澄清您的问题,将会有所帮助。
I don't quite see what the foreign keys are but assuming that a_tags.a_id maps to tags.id then try this. 我不太清楚外键是什么,但是假设a_tags.a_id映射到tag.id,然后尝试一下。
SELECT tags.tag,COUNT(a_tags.a_id) FROM a_tags
JOIN tags ON a_tags.a_id = tags.id
GROUP BY a_tags.a_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.