Problems with inner join SQL - php

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

Related

Count row when left join returns multiple for row

I'm selecting rows with left join. t2 can return mulitple rows based on the where clause.
This query returns the count from the t2 table, how do I count the result from t1. I've tried added group by t1.id, but this only returns 1.
Thanks!
SELECT COUNT(t1.id) AS number_of_rows
FROM t1
LEFT JOINt2 ON t1.id=t2.t1_id
WHERE t2.row='?' OR t2.row='?'
SELECT COUNT(t2.id) AS number_of_rows
FROM t1
LEFT JOIN (
SELECT * FROM t2 WHERE t2.row='?' OR t2.row='??' GROUP BY t1_id
) t2 ON t1.id=t2.t1_id
This seems to work for me. I was surprised I had to count t2.id and not t1.id. Can anyone explain me this?

MySQL INNER JOIN query issue

I have created an INNER JOIN query as shown below and was wondering how I can make it work? I need for the HomeTeam and AwayTeam to equal TeamID in the query. Any help would be much appreciated. Thanks
$result = mysqli_query($con,"SELECT results.*,team.TeamName
FROM results
INNER JOIN team ON team.TeamID = results.HomeTeam
INNER JOIN team on team.TeamID = results.AwayTeam");
You need to use aliases for the table that you are including twice. Otherwise mysql cannot distinguish between the two.
To be able to process the results easily, you can do the same with the names you are selecting.
Something like:
SELECT
results.*,
t1.TeamName AS TeamNameHome,
t2.TeamName AS TeamNameAway
FROM results
INNER JOIN team t1
ON t1.TeamID = results.HomeTeam
INNER JOIN team t2
ON t2.TeamID = results.AwayTeam

MySQL: If a column value is 2 THEN do X ELSE do Y

The database I am working on right now is somewhat messy. I have three potential tables, that I want to join but in some cases it may only be two tables.
Let's call these table1, table2 and table3.
table1 has a field called "type". If table1.type is 2, then I only need to join table3.
For any other values I want to join table2 and then table3.
How can I achieve this in one single SQL query rather than: 1) having one query to select the type. 2) make a PHP foreach-loop to check the type of the current iteration and 3) perform a new query according to the type value.
Edit:
I'll try to be more specific.
table1 has a column named "pid" that references to a whole other table, but that's redundant to this question. I tried working my ways around with UNIONs and LEFT JOINs but couldn't manage to achieve what I was looking for.
I want to select all results from my database with the "pid" value being "100". This gives me four rows in return, where was 2 of them are of type value "2" and the others are "1".
So basically what I want to achieve is the following two SQL statements in one:
(If "type" is "2")
SELECT *
FROM table1 t1
INNER JOIN table3 t3
ON t1.id = t3.t1_id
WHERE t1.pid = 100
(If "type" is NOT "2")
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON t1.id = t2.t1_id
INNER JOIN table3 t3
ON t2.id = t3.t2_id
WHERE t1.pid = 100
I'm guessing I could manage to do this with a UNION statement, but I'm confused on how to implement the WHERE t1.pid = '100' part.
use an UNION e.g.
SELECT t1.*, t3.*
FROM table1 t1
INNER JOIN table3 t3 ON t1.id = t3.t1_id
WHERE t1.pid = 100 and t1.type = 2
UNION
SELECT t1.*, t3.*
FROM table1 t1
INNER JOIN table2 t2
ON t1.id = t2.t1_id
INNER JOIN table3 t3 ON t2.id = t3.t2_id
WHERE t1.pid = 100 and t1.type <> 2;
but it would be better to explicitly name the columns you want to get back.

write mysql query using inner joins for three tables

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

Counting MySQL GROUP BY

So, I have next table structure :
Is there a way to make SQL query that counts simillar hashes in r_hash column, than founds this hash in hash column and returns an uid, and count of hashes?
For example - uid - 21520578; type - 1; count - 7?
Try the below query
SELECT T1.uid, T1.type, T2.count
FROM table T1,
(
SELECT r_hash, COUNT(*) AS count
FROM table
GROUP BY r_hash
) T2
WHERE T1.hash = T2.r_hash
You can do that by using join
SELECT t1.uid, t1.type, COUNT(t2.id) as `count`
FROM table AS t1
LEFT JOIN table AS t2 ON t2.r_hash = t1.hash
GROUP BY t1.id
I am not tested this query.
Edit: with left join you also receive rows with count = 0.

Categories