get number of rows in join query - php

I am saving Item and order in different tables. i want to count order as per item.
Here is my table structure.
Table1: Order table
id | table2_id
1 | 1
2 | 1
3 | 2
4 | 2
Table2: Item Table
id | user_id
1 | 1
2 | 2
3 | 1
4 | 2
One item has multiple order as shown in the table structure. Now i want to count the order as each item.
I have tried using join but it gives me all row cont: 4
SELECT count(Table1.id) as order_count_for_each_item
FROM `Table2` as `t2`
LEFT OUTER JOIN `Table1` as `t2` ON `t1`.`id` = `t2`.`table2_id`;
But i want each item count: as For Item1 is: 2 and Item2 is: 2
The conclusion:
As Rahul Suggested this:
SELECT count(*) as order_count_for_each_item
FROM `Table2` as t2
LEFT OUTER JOIN `Table1` as t1 ON `t1`.`table2_id` = `t2`.id
GROUP BY t1.`table2_id`
This query give the me result which i want initially.
order_count_for_each_item
1 |
2 |
But the answer i accepted (Bibhudatta Sahoo) gives me the item count which is zero and also the count of item par order.
SELECT t2.`id`,
(SELECT count(t1.id) form `Table1` as t1
where t2.`id` = t1.`table2_id`) as order_count_for_each_item
FROM `Table2` as t2
group by t2.`id`
order by t2.`id` asc
Item ID | order_count_for_each_item
1 | 2
2 | 2
3 | 0
4 | 0
So I accepted that answer.

Try Like this
SELECT t2.`id`,
(SELECT count(t1.id) form `Table1` as t1
where t2.`id` = t1.`table2_id`) as order_count_for_each_item
FROM `Table2` as t2
group by t2.`id`
order by t2.`id` asc

You are missing a group by here
SELECT count(*) as order_count_for_each_item
FROM `Table2` as t2
LEFT OUTER JOIN `Table1` as t1 ON `t1`.`table2_id` = `t2`.id
GROUP BY t1.`table2_id`

You will have to use an aggregate function Group by. group your data over the item and get the count.
SELECT item.id,ifnull(count(order.id),0) as order_count_for_each_item
FROM `Table2` as `item`
LEFT JOIN `Table1` as `order` ON order.table2_id=item.id
group by item.id ;

Related

how to get data from both table with join

Table 1 : Main_Family_Member
ID | Name
1 | Mahesh
2 | Rahul
3 | Jay
Table 2 : Family_Members
ID | MainMember | Name
1 | 1 | 'Arun'
2 | 1 | 'Nitin'
3 | 2 | 'Pratik'
Want Result :
Name
Mahesh
Arun
Nitin
Rahul
Pratik
You can achieve this by doing a UNION ALL of the two tables along with proper ordering. Note that it is necessary to union the two tables joined, because we need to know whether a main family member has any members. In case he does not have any member, your sample output implies that you don't want to display that main family member at all.
SELECT t.Name
FROM
(
SELECT DISTINCT t1.ID, t1.Name, 0 AS position
FROM
(
SELECT t1.ID, t1.Name
FROM Main_Family_Member t1
INNER JOIN Family_Members t2
ON t1.ID = t2.MainMember
WHERE t2.ID IS NOT NULL
) t1
UNION ALL
SELECT t2.ID, t2.Name, 1 AS position
FROM
(
SELECT t2.MainMember AS ID, t2.Name
FROM Main_Family_Member t1
INNER JOIN Family_Members t2
ON t1.ID = t2.MainMember
WHERE t2.ID IS NOT NULL
) t2
ORDER BY ID, position, Name
) t
Demo here:
SQLFiddle
SELECT Main_Family_Member.Name, Family_Members.Name
FROM Main_Family_Member
INNER JOIN Family_Members
ON Main_Family_Member.ID = Main_Family_Member.MainMember;
SELECT Main_Family_Member.Name, Family_Members.Name
FROM Main_Family_Member
INNER JOIN Family_Members
ON Main_Family_Member.ID = Main_Family_Member.MainMember;
You will need to perform a INNER JOIN on the two tables. This would return all the rows in the first table that meet the conditions plus all rows on the second table that join with the first table on the unique fields.
SELECT Main_Family_Member.Name , Family_Members.Name FROM Main_Family_Member INNER JOIN Family_Members ON Main_Family_Member.ID = Family_Members.MainMember WHERE Main_Family_Member.ID = 1 OR Main_Family_Member.ID = 2
SELECT Main_Family_Member.Name,Family_Members.Name FROM Main_Family_Member
INNER JOIN Family_Members ON Main_Family_Member.ID=Family_Members.MainMember

How to join this 5 tables in mysql?

I have 5 tables that I need to connect to get the necessary data.
Table1
id | number
1 | 1
2 | 5
Table 2
id | number | user_id
1 | 1 | 9
2 | 5 | 8
Table 3
id | name |
8 | john |
9 | jane |
Table 4
id | email
6 | johndoe#example.com
Table 5
id | table4_id | table3_id
1 | 6 | 8
Table 1 is my main table and I want to add the name and email from table 3 and 4 respectively to my select query of table 1, but in order to do so, I would need to use table 2 and 5 to connect them as there is no direct relationship between table 1 and table 3 and 4. I only know how to join 3 tables and not 5 tables and it seems confusing to me on how to proceed with this.
I followed the link here to join Table 1,2 and 3. But I don't know how to proceed with table 4 and 5.
This is the query I tried:
SELECT table1.number, table2.number, table2.user_id, table3.id, table3.name,
table4.id, table4.email, table5.table4_id, table3_id
FROM table1
LEFT JOIN table2
INNER JOIN table3
//this query will work if I don't include this 2 inner joins
INNER JOIN table4
INNER JOIN table5
ON table3.id = table5.table3_id
ON table5.table4_id = table4.id
//
ON table2.user_id= table3.id
ON table2.number = table1.number;
ERROR: (if included the inner join for table 4 and 5)
Error Code: 1064. You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'ON table2.user_id= table3.id
The right Syntax is select .. from .. join .. on .. join ..on ....
SELECT table1.number, table2.number, table2.user_id, table3.id, table3.name, table4.id, table4.email, table5.table4_id, table3_id
FROM table1
LEFT JOIN table2 ON table2.number = table1.number
INNER JOIN table3 ON table2.user_id= table3.id
INNER JOIN table5 ON table3.id = table5.table3_id
INNER JOIN table4 ON table5.table4_id = table4.id
Try with the below structure. I made it from the structure you given in question
select t1.number, t2.number, t2.user_id, t3.id, t3.name,t4.id, t4.email, t5.t4_id, t3_id
from table5 as t5
join table4 as t4 on t5.table4_id = t4.id
join table3 as t3 on t5.table3_id=t3.id
join table2 as t2 on t2.user_id = t3.id
join table1 as t1 on t2.number=t1.number
Try Below code. Hope this will work.
Select t3.name,t4.email,t1.number,t2.user_id
from table3 t3 JOIN table5 t5 ON t3.id=t5.table3_id
JOIN table4 t4 ON t4.id=t5.table4_id
JOIN table2 t2 ON t2.user_id=t3.id
JOIN table1 t1 ON t1.number=t2.number;

MySQL union query, order by 2 variables

Each table (table1 & table2) has its own DATETIME field.
I'm trying to catch id's of the two tables and order them by their DATETIME field.
Example:
Table 1 Table 2
------------ -------------
id | datetime1 id | table1id | datetime2
------------------------ -----------------------
1 | 2014-09-21 20:31:26 1 | 2 | 2014-09-21 20:31:29
2 | 2014-09-21 20:31:27 2 | 3 | 2014-09-21 20:31:30
3 | 2014-09-21 20:31:28
Table 3
------------
id | user
------------------------
2 | phil
3 | nathalie
My output isn't ordered properly with this query:
SELECT *
FROM (
SELECT
1 AS selection,
table1.id, table1.datetime1,
table2.datetime2
table3.user
FROM Table1
LEFT OUTER JOIN table2
ON table1.id = table2.table1id
LEFT OUTER JOIN table3
ON table1.id = table3.id
UNION ALL
SELECT
2 AS selection,
table1.id, table1.datetime1,
table2.datetime2
table3.user
FROM Table1
INNER JOIN table2
ON table1.id = table2.table1id
INNER JOIN table3
ON table1.id = table3.id
) AS query
ORDER BY table1.datetime1 DESC, table2.datetime2 DESC
Desired data:
from table 2 id: 2, 1,
from table 1 id: 3, 2, 1
So: 2, 1, 3, 2, 1
////EDIT
To people who could be struggling with long and complex MySQL request, please try it in PhpmyAdmin! It will tell you the error!
////EDIT
What you really need to do is to consider your schema more carefully. Consider naming the date time columns the same and then running a query like this - http://sqlfiddle.com/#!2/a3b4c/7/0
SELECT selection, id, datetimefoo, user FROM (
SELECT
1 AS selection,
table1.id, table1.datetimefoo,
table3.user
FROM table1
LEFT OUTER JOIN table2
ON table1.id = table2.table1id
LEFT OUTER JOIN table3
ON table1.id = table3.id
UNION
SELECT
2 AS selection,
table1.id, table1.datetimefoo,
table3.user
FROM table1
INNER JOIN table2
ON table1.id = table2.table1id
INNER JOIN table3
ON table1.id = table3.id
) AS T2
ORDER BY datetimefoo DESC
In the SQL fiddle this produces the results closer to what you're looking for. I am still not sure why you need the INNER JOINS on the second query though - there is nothing that you're doing here whcih requires them.
Here is another method that does not require a changing of the column names, but requires an alias for the sortable columns - http://sqlfiddle.com/#!2/ec4bc/3/0
SELECT * FROM (
SELECT
1 AS selection,
table1.id, table1.datetimefoo AS sort_date, -- alias on first table's date
table2.datetimebar,
table3.user
FROM table1
LEFT OUTER JOIN table2
ON table1.id = table2.table1id
LEFT OUTER JOIN table3
ON table1.id = table3.id
UNION
SELECT
2 AS selection,
table1.id, table1.datetimefoo,
table2.datetimebar AS sort_date, -- alias on second table's date
table3.user
FROM table1
INNER JOIN table2
ON table1.id = table2.table1id
INNER JOIN table3
ON table1.id = table3.id
) AS T2
ORDER BY sort_date DESC
I believe you are over-complicating a rather straight-forward task:
SELECT *
FROM (
SELECT 1 AS selection, table1.id as id, table1.datetime1 as date FROM Table1
UNION ALL
SELECT 2 AS selection, table2.id as id, table2.datetime2 as date FROM Table2
) AS query
ORDER BY date DESC

Slow query using "where in" and "order by select count"

I want to show post from users that specified user is followed and i have two tables at below. but its query is very slow.
table user
id | username
1 | name1
2 | name2
3 | name3
..
..
table post
id | poster_id | post_content
1 | 2
2 | 3
3 | 10
..
..
table follow
followerid | followtoid
1 | 2
1 | 3
2 | 10
..
..
Assume that all tables have more than 1000 rows.
This's SQL
SELECT *
FROM post
WHERE poster_id IN (
SELECT followtoid
WHERE followerid = $_SESSION['userid']
)
And this's the second cast is very slow too.
I want to list all member by order from their total posts.
SELECT *
FROM user
ORDER BY (
SELECT COUNT(id)
FROM post
WHERE post_id = user.id
) DESC;
Try indexing post.userid, post.poster_id, followtoid.followerid and user.user_id, using CREATE INDEX, and use LEFT JOIN clause on your queries instead:
SELECT *
FROM user u
LEFT JOIN SELECT poster_id, COUNT(*) as count FROM post p GROUP BY poster_id
ON (u.user_id = p.poster_id)
ORDER BY count DESC;
and:
SELECT * FROM post AS p
LEFT JOIN (SELECT followerid FROM followtoid) AS f
ON (p.userid=f.followerid)
WHERE p.userid = {$_SESSION['userid']}
Use a JOIN for the first query
SELECT p.*
FROM post p
JOIN follow f ON p.post_id = f.followtoid
WHERE f.followerid = $_SESSION['userid']
and a JOIN plus a GROUP BY for the second
SELECT u.*, tbl.postCount
FROM user u
JOIN (
SELECT poster_id, COUNT(*) AS postCount
FROM post p
GROUP BY posterID
) tbl ON tbl.poster_id = u.id
ORDER BY postCount DESC
You can accomplish the second query without a subquery:
SELECT u.*, COUNT(p.poster_id) as postCount
FROM user u
LEFT JOIN post p
ON (u.user_id = p.poster_id)
GROUP BY u.user_id
ORDER BY postCount DESC;

mysql query on two tables

What SELECT query can i use to only show the cars WHERE bookings.status <> 1
so on the table below, cars.id (1, 3, 4, 6) will only show as the result
i'm stuck with this query or of this is any good:
SELECT * FROM `cars` as `C` INNER JOIN `bookings` AS `B` ON `C`.`id` = `B`.`id` ....?
cars
id | name
-- | -------------
1 | Car 1
2 | Car 2
3 | Car 3
4 | Car 4
5 | Car 5
6 | Car 6
bookings
id | car_id | status
-- | ------ | ------
1 | 1 | 0
2 | 2 | 1
3 | 2 | 2
4 | 1 | 0
5 | 5 | 1
EDIT: sorry i wasn't clear here, i also want the others listed as result even though they are not on the bookings table
Try this :
SELECT * FROM `cars` as `C` INNER JOIN `bookings` AS
`B` ON `C`.`id` = `B`.`id` where `B`.`status` <> 1
Try this if you didn't want records from Cars that are not in Bookings :
SELECT * FROM `Cars` as `C` Right JOIN `Booking` AS
`B` ON `C`.`id` = `B`.`id`
This will only show the cars
SELECT c.name FROM `cars` as `C` INNER JOIN `bookings` AS
`B` ON `C`.`id` = `B`.`id` where `B`.`status` != 1
try this
SELECT * FROM `cars` C, `bookings` B WHERE C.id=B.car_id AND B.status!=1
Try This...
SELECT * FROM cars C LEFT JOIN bookings B ON C.id = B.car_id WHERE B.status <> 1
Assuming your car_id from your bookings table is the same column as the id in the cars table...
SELECT A.* from cars A
inner join bookings B on A.id=B.car_id
where b.status<>1;
SOLVED IT with this query
SELECT * FROM `cars` WHERE `id` NOT IN (SELECT `B`.`car_id` FROM `cars` AS `C` INNER JOIN `bookings` AS `B` ON `C`.`id` = `B`.`car_id` WHERE `B`.`status` = 1)
thanks everyone, it helped alot from your answers
SELECT A.* , B.* FROM cars A, bookings B WHERE B.car_id=A.id AND B.status <> 1

Categories