Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8400093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:28:32+00:00 2026-06-09T21:28:32+00:00

I need to get data from multiple tables from a database and I need

  • 0

I need to get data from multiple tables from a database and I need to use 1 query, but I can’t get it to work.

I got these table:
projects:

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

users

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

tasks

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

So what I need to do, is make a query that should give me this result:
Result:

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  

I got it to work until the part where I need to count the amount of tasks that a project has.
I got this query, but that doesn’t work:

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 dump, so people can try to test their query for me:

-- 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 */;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:28:34+00:00Added an answer on June 9, 2026 at 9:28 pm
    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to get data from several tables, so I used a query with
I have 3 tables, I need advice on how to get data from them.
I have huge amount of data from database, i need to get or store
I have a very basic need to get some data from the database and
Greetings, Here is my problem. I need to get data from multiple rows and
I need to search across multiple columns from two tables in my database using
Is there a best practice in getting data from multiple database tables using Zend?
I need to get data from xml file <?xml version=1.0?> <xml> <User> <Agent> <id>cr4523</id>
I need to get data from flat files to DB using SSIS . The
I am working on a C# project where I need to get data from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.