write mysql query using inner joins for three tables - php

I have the following query which works perfectly:
SELECT *
FROM contacts
WHERE id in (
SELECT DISTINCT contacts.id
FROM contacts
INNER JOIN contacts2tags
ON contacts.id = contacts2tags.contactid
WHERE tagid in(7,4)
)
Here contacts table contains id, first_name, last_name, ..and tags table contains id, name. contacts2tags table contains contactid and tagid which are same as contacts.id and tags.id respectively
Now, what I want is, to display only the contacts which have both a tagid 7 and a tagid 4.
I tried something like this:
SELECT *
FROM contacts
WHERE id IN
(
SELECT CT1.contactid
FROM
tags T1, contacts2tags CT1, tags T2, contacts2tags CT2
WHERE CT1.contactid = CT2.contactid
AND CT1.tagid = T1.id
AND CT2.tagid = T2.id
AND (T1.id = 7 AND T2.id = 4)
and it works too.
My problem is, I want to convert the above second query to one using inner joins.
I have an array of ids stored in $tmp in php
I want to use those ids and write the above query for them.
How do I do that? I am not comfortable with sql. Might be its a very simple thing to ask.
Thanks in advance
EDIT:
The answer below solved the problem. But the sql runs very slow for 10k records. Any suggestions to optimise it? Pasting the updated query as given in the answer.
SELECT c.id
FROM contacts c
inner join contacts2tags t on c.id = t.contactid
where t.tagid in (7,4)
group by c.id
having count(distinct t.tagid) = 2

This should work
SELECT c.id
FROM contacts c
inner join contacts2tags t on c.id = t.contactid
where t.tagid in (7,4)
group by c.id
having count(distinct t.tagid) = 2

Related

Error joining 3 tables php/sql

I'm new to this, so I know I'm missing something simple, but I can't figure it out. I'm trying to join 3 tables together and I've got it working with 2 joins, but when combined in the same query, there ends up being an error.
My 3 tables are:
TBL_Authors
Author_ID
Author_Name
TBL_Publishers
Publisher_ID
Publisher_Name
TBL_Books
Title
Author_ID
Publisher_ID
ISBN
Genre
Price
Cost
Rating
What I have that isn't working:
$query = 'SELECT * FROM TBL_PUBLISHERS
JOIN TBL_BOOKS ON TBL_PUBLISHERS.Publisher_ID = TBL_BOOKS.Publisher_ID
SELECT * FROM TBL_AUTHORS
JOIN TBL_BOOKS ON TBL_AUTHORS.Author_ID = TBL_BOOKS.Author_ID
ORDER BY TBL_BOOKS.Title ASC;';
This query assumes that each book was published.
SELECT
*
FROM
TBL_Books b
INNER JOIN TBL_Publishers p ON b.Publisher_ID = p.Publisher_ID
INNER JOIN TBL_Authors a ON b.Author_ID = a.Author_ID
ORDER BY
b.Title
Book will always have an Author, but not necessarily a Publisher, if it's not published. If you need to fetch all the books irrespective of whether published or not, you will have to change INNER join on TBL_Publishers to LEFT.
This Query Will show you the result of all details of book, publisher name, author name.
SELECT
t1.* , t2.Publisher_Name , t3.Author_Name
FROM
TBL_Books as t1
INNER JOIN TBL_Publishers as t2 ON t1.Publisher_ID = t2.Publisher_ID
INNER JOIN TBL_Authors as t3 ON t1.Author_ID = t2.Author_ID
ORDER BY
t1.Title
Check This and update if this query helps you.

Problems with inner join SQL

Basically i am working with 2 tables, one is the cars and one is comments and i am trying to get the count of approved comments for certain cars + the cars information, but i would like it to be dynamic if i put in the id of the car directly it will work but when i say where id = car id it returns nothing?
This works with "10" it will give me the information + the count of comments
SELECT t1.biler_id, t1.biler_navn, COUNT(t2.bilbixen_commentary_status)
FROM bilbixen_biler t1
INNER JOIN bilbixen_commentary t2 ON t1.biler_id = t2.bilbixen_commentary_bil
WHERE t1.biler_id = 10
The above query will return this in phpmyadmin
The below query don't work, i am trying to tell it to find everything where t1.id is equal t2.id
SELECT t1.biler_id, t1.biler_navn, COUNT(t2.bilbixen_commentary_status)
FROM bilbixen_biler t1
INNER JOIN bilbixen_commentary t2 ON t1.biler_id = t2.bilbixen_commentary_bil
WHERE t1.biler_id = t2.bilbixen_commentary_bil
The above query will give me this error in phpmyadmin
I expected it to return all the information for each car with a count of comments for each of them
Your where clause is useless and you need a group by clause since you are using the aggregate function count.
select t1.biler_id,
t1.biler_navn,
COUNT(t2.bilbixen_commentary_status)
from bilbixen_biler t1
left join bilbixen_commentary t2 on t1.biler_id = t2.bilbixen_commentary_bil
group by t1.biler_id,
t1.biler_navn
You have used the aggregate function COUNT so you have to group by t1.biler_id, t1.biler_navn fields:
SELECT t1.biler_id, t1.biler_navn, COUNT(t2.bilbixen_commentary_status)
FROM bilbixen_biler t1
INNER JOIN bilbixen_commentary t2 ON t1.biler_id = t2.bilbixen_commentary_bil
GROUP BY t1.biler_id, t1.biler_navn

Combine two mysql group queries

I'm making a web app to create tournaments and as i have learned PHP in the course of this project, so my skills aren't probably the best.
I have an identifier in my database day2_semifinal or day2_additional which basically identifies the type of semifinal.
So my first query is:
$numberquery = mysql_query("
SELECT *
FROM tourneyplayers
INNER JOIN results
on (resultid=r_id)
INNER JOIN players
ON (p_id=playerid)
INNER JOIN tourneys
on (T_Id=tourneyid)
WHERE tourneyid='$tourneyid' and
in_day2 = 1 and
day2_semifinal IS NOT NULL
GROUP BY day2_semifinal
ORDER BY agegroupid",$connection);
This will get me all the semifinal groups, i'll iterate over them and query all the players in group:
$semigroup = $group['day2_semifinal'];
$playerQuery = mysql_query("
SELECT *
FROM tourneyplayers
INNER JOIN results
on (r_id=resultid)
INNER JOIN players
on (p_id=playerid)
WHERE tourneyid='$tourneyid' AND
day2_semifinal = '$semigroup' and
in_day2 = 1
ORDER BY day2startplace",$connection);
Now after i've created tables and echoed all the data from player queries for day2_semifinal, i run another query:
$numberquery = mysql_query("SELECT * FROM tourneyplayers INNER JOIN results on (resultid=r_id) INNER JOIN players ON (p_id=playerid) WHERE tourneyid='$tourneyid' and in_day2 = 1 and day2_additional_nosemi IS NOT NULL AND day2_additional_nosemi <> 0 GROUP BY day2_additional_nosemi ORDER BY agegroupid",$connection);
Which is fairly similar to the first one, only thing different is day2_semifinal identifiers have changed to day2_additional. After that query, i'll again, iterate over the day2_additional_nosemi groups and query the players inside of them:
$additionalgroup = $group['day2_additional_nosemi'];
$playerQuery = mysql_query("SELECT * FROM tourneyplayers INNER JOIN results on (r_id=resultid) INNER JOIN players on (p_id=playerid) WHERE tourneyid='$tourneyid' AND day2_additional_nosemi = '$additionalgroup' and in_day2 = 1 ORDER BY day2startplace",$connection);
Now this works, but this creates an issue with ordering, since the first query orders them by agegroupid but only for players in day2_semifinal (and i'd like to have day2_additional players ordered together with day2_semifinal). If i run another query the previous data has already been echoed and ordering is not right. How could i concatenate two $numberquery queries in order to select players after them as well?
I'm answering my own question as i figured out a way to do this. What i did, was removed ORDER BYfrom both queries and created a new query which concatenated the two with UNION:
SELECT * FROM (
SELECT *
FROM tourneyplayers as tp1
INNER JOIN results as r1
on (tp1.resultid=r1.r_id)
INNER JOIN players as p1
ON (p1.p_id=tp1.playerid)
WHERE tp1.tourneyid=96 and
tp1.in_day2 = 1 and
r1.day2_semifinal IS NOT NULL
GROUP BY r1.day2_semifinal
UNION ALL
SELECT *
FROM tourneyplayers as tp2
INNER JOIN results as r2
on (tp2.resultid=r2.r_id)
INNER JOIN players as p2
ON (p2.p_id=tp2.playerid)
WHERE tp2.tourneyid=96 and
tp2.in_day2 = 1 and
r2.day2_additional_nosemi IS NOT NULL AND
r2.day2_additional_nosemi <> 0
GROUP BY r2.day2_additional_nosemi
) t ORDER BY t.agegroupid;

Mysql 2 tables use player_id with name from other table

I am new to php & mysql and I'm trying to make a script that gets the distance walked with the player's name. I can get the player's walked distance with his id, but the value for the player_id is in a different table.
It looks like this:
Table1: player_id | foot (walked distance)
Table2: name | player_id
So I want to use the name by the player_id in my table.
Code
You require a simple join.
SELECT Table1.foot, Table2.name
FROM Table1
INNER JOIN Table2
ON Table1.player_id = Table2.player_id;
You just need to join both these table.
Just try this code:
$query = "SELECT T1.*, T2.name
FROM table1 T1
LEFT JOIN table2 T2 ON T1.player_id = T2.player_id
ORDER BY T2.name ASC";
For more details of JOIN: Link
Let me know for more help.
You can use
$query = "select t1.player_id, t2.name, t1.foot
from table1 t1
join table2 t2 on t1.player_id = t2.player_id"
If you want to order the player names in alphabetical order then you can additionally use order by clause
$query = "select t1.player_id, t2.name, t1.foot
from table1 t1
join table2 t2 on t1.player_id = t2.player_id
order by t2.name"
Use left join in mysql.
Suppose if you have two tables use this
SELECT T1.*,T2.walked distance
FROM table1 T1
LEFT JOIN table2 T2
ON T1.id=T2.player_id;
Click Here For more example

SQL Query for finding what results are not in a table

I am a little bit rusty on my SQL and would like some help with these queries that I am struggling getting my head round or even finding out if it is possible.
Hi so I have some tables;
clients,products,clientproducts
when a client opts in for a product that is added to clientproducts, using the clientid and the productid.
the easy query
I want to run a query that will only show me products the client has opted in for.
the harder one
I also want to run a query that will show me just the clients that haven't opted in for a product/s.
Thank you for your help in advance.
Query 1:
SELECT table1.id1 FROM table1
WHERE table1.id1 IN (SELECT table2.foreign_id FROM table2);
Query 2:
SELECT table1.id1 FROM table1
WHERE table1.id1 NOT IN (SELECT table2.foreign_id FROM table2);
here, use LEFT JOIN
SELECT a.*
FROM products a
LEFT JOIN clientproducts b
ON a.productID = b.productID
LEFT JOIN clients c
ON b.clientID = c.clientID AND
c.ClientID = 'clientID HERE'
WHERE c.client IS NULL
OR
SELECT a.*
FROM products a
LEFT JOIN clientproducts b
ON a.productID = b.productID AND
b.ClientID = 'clientID HERE'
WHERE b.productID IS NULL
The easy query
SELECT * FROM clients LEFT JOIN products ON clients.id = products.client_id
The hard query
SELECT * FROM clients WHERE clients.id NOT IN (SELECT clients.id FROM clients LEFT JOIN products ON clients.id = products.client_id)
Get all the products information where client_id = 1
select products.*
from products, clientproducts
where products.id = clientproducts.product_id
and clientproducts.client_id = 1
Get all the clients details who has not opted for any products
select *
from clients
where id not in (select client_id
from clientproducts)

Categories