mysql query on two tables - php

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

Related

left join and return only the latest record from right table using LARAVEL 6.5.0

Table 1
----------
NameID Name
------------------
1 A
2 B
3 C
-----------------
Table 2
--------------------
ID NameID Order
--------------------
1 1 Sugar
2 1 Salt
3 2 Garlic
4 2 Sugar
5 2 Onion
6 3 Oil
7 3 Black pepper
I want to return only the latest and only one record per nameID from
right table I dont know what code to use
This is the Result I want to return
RESULT
----------------------------
Name Order
---------------------------
A Salt
B Onion
C Black pepper
Controller.php
return DB::table('table1')
->leftjoin('table2','table1.nameID','=','table2.nameID')
-get();
try this
$data = Table1::select('Table1.Name', 'Table2.Order','Table2.ID')
->leftJoin('Table2', function($query) {
$query->on('Table1.NameID','=','Table2.NameID')
->whereRaw('Table2.ID IN (select MAX(a2.ID) from Table2 as a2 join Table1 as u2 on u2.NameID = a2.NameID group by u2.NameID)');
})->get();
Edited :
$data = Table1::select('Table1.Name', 'Table2.Order','Table2.ID')
Use not exists to filter
select Name, Order
from Table1 a
inner join
(
Select a.NameID, Order from Table2 a
where not exists(select 1 from Table2 b where a.NameID = b.NameID and a.ID < b.ID)
)b on a.NameID = b.NameID
You can try this below script-
SELECT B.NameID, B.Name, C.[Order]
FROM
(
SELECT Nameid,MAX(ID) ID
FROM table_2
GROUP BY NameID
)A
INNER JOIN Table_1 B ON A.NameID = B.NameID
INNER JOIN Table_2 C ON A.NameID = C.NameID AND A.ID = C.ID

get number of rows in join query

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 ;

mysql count number of votes in votes table

I have 3 tables named election_cand, candidate and votes:
election_cand table:
ele_can_id | election_id | candidate_id
candidate table:
id | canname | canadd | canphone | canmail | candes | canphoto
votes table:
voteid | candidateid | voterid | electionid
my tables with data are:
I want the desired result as
I write the mysql query as:
SELECT a.*,b.*,c.*, count(voteid) AS numrows
FROM election_cand a
left join votes b on a.election_id=b.electionid
left join candidate c on a.candidate_id=c.id
where a.election_id='$get_ele_id' group by a.candidate_id
As I see you only need this query:
SELECT
c.*, COUNT(e.ele_can_id) AS numrows
FROM candidate c
Left Join election_cand e on c.id = e.candidate_id
GROUP BY c.id
describe your schema so we can help you more.
Try this code:
SELECT
c.*, COUNT(v.candidateid) FROM candidate c
LEFT JOIN votes v ON c.id = v.candidateid
WHERE c.id IN
( SELECT candidate_id FROM election_cand WHERE election_id = 4 )
GROUP BY v.candidateid

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;

Categories