Using INNER JOIN twice in the same query - php

I'm still trying to get the hang of SQL. I built 1 query that pulls out thread_id's from a filter_thread(contains 'filter_id' and 'thread_id' columns) table and a filter('filter_id and 'tag') table'
Then, I use an entirely different query to find the thread table contents, which contains the 'thread_id.'
I realize this isn't the best way to do it, but can't get a successful query. Would anyone be able to help?
$query = "SELECT ft0.thread_id
FROM filter f0
INNER JOIN filter_thread ft0 ON ft0.filter_id = f0.filter_id
WHERE f0.tag LIKE '%filter1%'
OR f0.tag LIKE '%filter2%'"
$result = $query->result_array();
$thread = array();
foreach ($result as $thread_id)
{
$id = $thread_id['thread_id'];
$query = $this->db->query("SELECT * FROM thread WHERE thread_id='$id'");
$thisRow = $query->result_array();
array_push($thread, $thisRow[0] );
}
THANKS!

You can do it in a single query, like so:
SELECT t.*
FROM filter AS f0
INNER JOIN filter_thread AS ft0
ON ft0.filter_id = f0.filter_id
INNER JOIN thread AS t
ON ft0.thread_id = t.thread_id
WHERE f0.tag LIKE '%filter1%'
OR f0.tag LIKE '%filter2%

select t.x, t.y, ... from thread t
inner join filter f on f.thread_id = t.thread_id
inner join filter_thread ft on ft.filter_id = f.filter_id
where bla bla
With maybe some group by...?

Try this
SELECT t.*, f0.*
FROM filter f0
INNER JOIN filter_thread ft0 ON ft0.filter_id = f0.filter_id
INNER JOIN thread t ON t.thread_id = ft0.thread_id
WHERE f0.tag LIKE '%filter1%'
OR f0.tag LIKE '%filter2%'"
GROUP BY f0.filter_id

Related

MySQL INNER JOIN IN not getting right result

I have this query in my php and it seems to be fetching the wrong data set from my db.
$querystring = "
SELECT a.*,
b.itemcolour,
b.itemcolourname
FROM itemorders AS a
INNER JOIN catalogueitemscolour AS b
ON a.colourid = b.colourid
WHERE a.colourid IN(SELECT colourid
FROM itemorders
WHERE orderid = 61)
";
Here is a picture of my results
Can I know why it's not selecting the specific orderID of 61?
You can try below -
SELECT a.*, b.itemColour,b.itemColourName FROM itemorders
AS a INNER JOIN CatalogueItemsColour AS b ON a.colourID = b.colourID WHERE
a.orderID = 61
Because you are not putting condition on orderID, rather putting it on colorID. What you actually want is this condition: WHERE a.orderID = 61.

How to make a fancy mysql join that can join three tables and detect if one table DOESNT have my item

php mysql query
I have multiple linked tables - I also have a table that only creates and entry if certian conditions exist so I would like to add that into my query to avoid having to go through thousands of query searches looking for this special case
here is my current query
$query = "SELECT a.UUID FROM contract a
INNER JOIN geoPoint b ON a.customer_UUID = b.customerUUID
WHERE b.garcom_UUID = '$garbCom'
AND b.city_UUID = '$city'";
I then go through each item that was returned (in the thousands)
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$sentdata = getothertable($row['UUID']); //checks if the item is in the table
$sent = $sentdata ['senttoGarcom'];
if($sent == 0) //if it wasn't found add it to my list
{
array_push($Contracts,$row['UUID']);
}
}
instead of all that I would like to just make it one query - pseduo code something like this
$query = "SELECT a.UUID FROM contract a
INNER JOIN geoPoint b ON a.customer_UUID = b.customerUUID
INNER JOIN contract_sales c ON a.UUID = c.contractUUID
WHERE b.garcom_UUID = '$garbCom'
AND b.city_UUID = '$city' AND c.DOESNOTEXIST";
this way I dont have to return thousands I will only be returned the few that are not yet in the contract_sales table and I can go about my business...
Appreciate any help!
just check for NULL rows of c with a outer join
$query = "SELECT a.UUID FROM contract a
INNER JOIN geoPoint b ON a.customer_UUID = b.customerUUID
LEFT OUTER JOIN contract_sales c ON a.UUID = c.contractUUID
WHERE b.garcom_UUID = '$garbCom'
AND b.city_UUID = '$city' AND c.contractUUID IS NULL ";
I think this is a left outer join problem
Have a look at this example. You specifically need to have a check for a null in a column in the table which you want to find the missing row rof
mysql left outer join
Sounds like a NOT EXISTS correlated subquery is what you need:
$query = "SELECT a.UUID FROM contract a
INNER JOIN geoPoint b ON a.customer_UUID = b.customerUUID
WHERE b.garcom_UUID = '$garbCom'
AND b.city_UUID = '$city'
AND NOT EXISTS (SELECT 1
FROM contract_sales c
WHERE c.contractUUID = a.UUID)";

Combining 2 queries into 1

I'm looking to optimize two queries into one, if possible.
My first query searches for all the authors of a lyrics... Then, for each author found, i want to find the total numbers of lyrics the author was involded in...
Right now, im executing the first query and for each row found, i'm launching another query to get the authors total lyrics he was involved... So, if there is 4 authors i will end up launching 4 more queries...
That is to many queries in my opinion. That is why i've decided to write here, so i can get help on how to optimize my query...
This is the query i'm executing to get the author(s) responsable for a lyrics:
$sql = "SELECT author.author_id, author.name
FROM track INNER JOIN lyrics_author ON track.lyrics_id = lyrics_author.lyrics_id
INNER JOIN author ON lyrics_author.author_id = author.author_id
WHERE track.track_id = $trackid ";
This is the query to get the total number of lyrics the author as writing:
$total = "SELECT lyrics_author.author_id, count(*) as total
FROM lyrics_author
WHERE lyrics_author.author_id = $author_id
GROUP BY lyrics_author.author_id";
This is a sample of the code:
<?php
$trackid = 5;
$sql = "SELECT author.author_id, author.name
FROM track INNER JOIN lyrics_author ON track.lyrics_id = lyrics_author.lyrics_id
INNER JOIN author ON lyrics_author.author_id = author.author_id
WHERE track.track_id = $trackid ";
$result_author = # $conn->query($sql);
while ($row_author = $result_author->fetch_assoc()) {
$author_id = $row_author['author_id'];
$total = "SELECT lyrics_author.author_id, count(*) as total
FROM lyrics_author
WHERE lyrics_author.author_id = $author_id
GROUP BY lyrics_author.author_id";
$result_total_lyrics = # $conn->query($total);
$t = $result_total_lyrics->fetch_assoc();
echo $t['total'];
$result_total_lyrics->free();
}
$result_author->free();
?>
Is it posible to optimize this query? If yes, how? Is there a link you could refer, so i can learn...
Thanks
Marco
SELECT
author.author_id,
author.name,
COUNT(DISTINCT more_tracks.lyrics_id) AS total
FROM track
INNER JOIN lyrics_author USING (lyrics_id)
INNER JOIN author USING (author_id)
LEFT JOIN lyrics_author AS more_tracks USING (author_id)
WHERE track.track_id = $trackid
GROUP BY author.author_id
That's confusing as heck. Why are you passing in a trackid as a lyricsid when you have a property called lyricsid??
Anyway
Select author.author_id, author.name, Count(*)
inner join
(SELECT lyrics_author.author_id
FROM lyrics_author
INNER JOIN tracks ON track.lyrics_id = lyrics_author.lyrics_id
WHERE track.track_id = $lyricsid";
) as lyricalauthors
inner join lyrics_author on lyrics_author.author_id = lyricalauthors.author_id
On author.author_id = lyricalauthors.author_id
Group By Author.author_id,author.name
I think ...

Combining 3 mysql queries into 1

I need to query two new table $res_info and $res_text... how can I combine all 3 queries into one. The first query $res grabs everything include the unique field m_id which is used in the two other tables. can this be done with a UNION?
$res = #mysql_query("SELECT *, DATE_FORMAT(m_lld,'%m/%d/%y')
AS m_lld_formatted
FROM social_members
WHERE m_user='$en[user]'");
if (#mysql_num_rows($res) == 0) call404();
$line = #mysql_fetch_assoc($res);
foreach ($line as $key => $value)
$en['m'.$key] = str_replace("\n",'<br/>',stripslashes($value));
$res_info = mysql_query("SELECT *,
FROM social_meminfo
WHERE m_id = '".$en['mm_id']."'");
$res_text = mysql_query("SELECT *,
FROM social_memtext
WHERE m_id = '".$en['mm_id']."'");
Based on your comments I think you are looking for one OUTER JOIN and another INNER JOIN like this:
SELECT sm.*, DATE_FORMAT(sm.m_lld,'%m/%d/%y') AS m_lld_formatted
FROM social_members sm
LEFT OUTER JOIN social_memtext smt ON (sm.m_id = smt.m_id)
JOIN social_meminfo smi ON (sm.m_id = smi.m_id)
WHERE sm.m_user = "$en['user']"
LEFT OUTER JOIN will take care of situation when table social_memtext does have matching entries.
//use mysql_real_escape_string for your $en['User']
SELECT sm.*, DATE_FORMAT(sm.m_lld,'%m/%d/%y')
AS m_lld_formatted
FROM social_members sm
JOIN social_meminfo smi ON(smi.m_id = sm.m_id)
JOIN social_memtext smt ON(smt.m_id = sm.m_id)
WHERE sm.m_user = "$en['user']"

LEFT JOIN and WHERE causing error

I have a nested mysql_query.
$resultSub = mysql_query("SELECT *
FROM ensembles
WHERE en_name = $name
LEFT JOIN ensemble_names on ensembles.en_name = ensemble_names.en_nm_ID
LEFT JOIN students on ensembles.en_stu = students.s_ID
LEFT JOIN part_names on ensembles.en_part = part_names.p_nm_ID
ORDER BY $sort $orderBy");
The query works fine without the WHERE clause, which I thought may be filtering out rows for the LEFT JOIN command, but that's not the case.
The WHERE clause should be placed after the LEFT JOINs:
$resultSub = mysql_query("SELECT *
FROM ensembles
LEFT JOIN ensemble_names on ensembles.en_name = ensemble_names.en_nm_ID
LEFT JOIN students on ensembles.en_stu = students.s_ID
LEFT JOIN part_names on ensembles.en_part = part_names.p_nm_ID
WHERE en_name = $name
ORDER BY $sort $orderBy");
Well, you put the WHERE clause in the wrong place.
Read the documentation.

Categories