gpt4 book ai didi

mysql - 不确定如何进行正确的 mySQL 查询以在 1 个查询中从多个表获取数据

转载 作者:行者123 更新时间:2023-11-29 14:19:19 26 4
gpt4 key购买 nike

我需要从数据库的多个表中获取数据,并且需要使用 1 个查询,但我无法让它工作。

我得到了这些表:
项目:

id    name        start_date           end_date             project_leader    finished
1 project_1 2012-08-01 00:00:00 2012-29-01 00:00:00 2 0

用户

id    username    password    email      status
1 user_1 pass_1 email_1 1
2 user_2 pass_2 email_2 1

user_has_project

userid   projectId
1 1

任务

id   project  description          end_date               user
1 1 test description 1 2012-29-01 00:00:00 1
2 1 test description 2 2012-29-01 00:00:00 1

所以我需要做的是进行一个查询,应该给我这个结果:
结果:

project_id  project_name    start_date           end_date             project_leader    finished       tasks
1 project_1 2012-08-01 00:00:00 2012-29-01 00:00:00 user_2 0 2

我让它一直工作,直到我需要计算项目的任务数量。我收到此查询,但这不起作用:

SELECT projects.id, projects.name, projects.start_date, projects.end_date,
projects.finished, users.username AS project_leader, COUNT(tasks.id) AS tasks
FROM projects, tasks
INNER JOIN user_has_project ON user_has_project.projectId = projects.id
INNER JOIN users ON projects.project_leader = users.id
WHERE user_has_project.userId = 1

SQL 转储,以便人们可以尝试为我测试他们的查询:

-- phpMyAdmin SQL Dump
-- version 3.4.5
-- http://www.phpmyadmin.net
--
-- Machine: localhost
-- Genereertijd: 20 aug 2012 om 19:42
-- Serverversie: 5.5.16
-- PHP-Versie: 5.3.8

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `project-deadline`
--

-- --------------------------------------------------------

--
-- Tabelstructuur voor tabel `projects`
--

CREATE TABLE IF NOT EXISTS `projects` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) DEFAULT NULL,
`start_date` datetime DEFAULT NULL,
`end_date` datetime DEFAULT NULL,
`project_leader` int(11) DEFAULT NULL,
`finished` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

--
-- Gegevens worden uitgevoerd voor tabel `projects`
--

INSERT INTO `projects` (`id`, `name`, `start_date`, `end_date`, `project_leader`, `finished`) VALUES
(1, 'Project 1', '2012-08-01 00:00:00', '2012-09-18 00:00:00', 1, 0);

-- --------------------------------------------------------

--
-- Tabelstructuur voor tabel `tasks`
--

CREATE TABLE IF NOT EXISTS `tasks` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`project` int(11) DEFAULT NULL,
`description` varchar(100) DEFAULT NULL,
`end_date` datetime DEFAULT NULL,
`user` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

-- --------------------------------------------------------

--
-- Tabelstructuur voor tabel `users`
--

CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(32) NOT NULL,
`email` varchar(100) NOT NULL,
`status` int(11) NOT NULL,
`timezone` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

--
-- Gegevens worden uitgevoerd voor tabel `users`
--

INSERT INTO `users` (`id`, `username`, `password`, `email`, `status`, `timezone`) VALUES
(1, 'DijkeMark', '37540da17c71d40c656b97b32c00f692', 'mark.dijkema@gmail.com', 1, 'UP1');

-- --------------------------------------------------------

--
-- Tabelstructuur voor tabel `user_has_project`
--

CREATE TABLE IF NOT EXISTS `user_has_project` (
`userId` int(11) NOT NULL,
`projectId` int(11) NOT NULL,
PRIMARY KEY (`userId`,`projectId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Gegevens worden uitgevoerd voor tabel `user_has_project`
--

INSERT INTO `user_has_project` (`userId`, `projectId`) VALUES
(1, 1),
(1, 6);

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

最佳答案

SELECT projects.id,
projects.name,
projects.start_date,
projects.end_date,
projects.finished,
users.username AS project_leader,
COUNT(tasks.id) AS tasks
FROM projects
LEFT JOIN tasks ON (tasks.project = projects.id)
JOIN user_has_project ON (user_has_project.projectId = projects.id)
JOIN users ON (projects.project_leader = users.id)
WHERE user_has_project.userId = 1
GROUP BY projects.id

关于mysql - 不确定如何进行正确的 mySQL 查询以在 1 个查询中从多个表获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12025886/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com