Combining 3 mysql queries into 1 - php

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']"

Related

PB / SQLite3 JOIN COMPARE TABLES

I have Two tables :
T1
"IdT1;i1T1,n2T1
1;123;n2T1
2;234;n3T1
3;345;n4T1
4;678;n1T1
5;123;n2T1
6;234;n3T1
T2
"idT2;n1T2;n2T2;i1T2
1;1n1T2;2n1T2;123
2;1n2T2;2n1T2;234
3;1n3T2;2n1T2;345
4;1n4T2;2n1T2;456
5;1n5T2;2n1T2;567
6;1n6T2;2n1T2;678
In SQLITE3
$req = $db -> prepare("SELECT T2.n1T2, T2.n2T2,
//COUNT(*)
FROM T1
INNER JOIN T2 ON T1.idT2 = T2.idT2
");
if ($res = $req->execute()) {
$arr = $res->fetchArray();
var_dump($arr);
}
I search for n1T2 and n2T2 names, the count of same values between i1T1 and i1T2
example:
1n1T2;2n1T2;2
1n2T2;2n2T2;2
1n3T2;2n1T2;1
...
I var_dump for display, in wait best method
It looks like you want a join and aggregation:
select t2.n1t2, t2.n2t2, count(*) cnt
from t1
inner join t2 on t2.i1t2 = t1.i1t1
group by t2.n1t2, t2.n2t2

sql, join 2 Tables

I have 2 tables
matchdata:
id_team1, id_team2, name_team1, name_team2, group_order_id
teams:
team_id, team_name
I try this:
select * from matchdata join teams on matchdata.id_team1 = teams.team_id;
But I need
matchdata.id_team1 = teams.team_id
AND
matchdata.id_team2 = teams.team_id
after that I want select WHERE group_order_id = $bla
How can I do that?
You can use two JOIN on a single table.
SELECT * FROM matchdata
JOIN teams t1 ON matchdata.id_team1 = t1.team_id
JOIN teams t2 ON matchdata.id_team1 = t2.team_id
WHERE group_order_id = $bla;
You can use IN for this:
SELECT *
FROM matchdata md
JOIN teams t ON t.team_id IN (md.id_team1, md.id_team2)
WHERE md.group_order_id = $bla

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)";

Add a filter to a large mysql query

I have this large query, and I just need to filter out any results where the tbl_dealinterest.Active = 'n'. There sometimes isn't an entry in that table for the product, and sometimes it might there might be and entry and set to y.
Here is the large ugly query:
SELECT tbl_product.id, tbl_productspecification.id AS specificationId,
tbl_product.ProductId, tbl_seller.CompanyName, tbl_product.ProductName, tbl_product.Description, mst_Categories.id AS 'Category',
tbl_productspecification.RetailPrice, tbl_productspecification.SalePrice,
tbl_product.image, tbl_productspecification.Discount, tbl_product.EndTime, tbl_product.Seller_Id, tbl_dealinterest.Active AS thumbsActive
FROM tbl_product
LEFT OUTER JOIN tbl_seller ON tbl_seller.SelId = tbl_product.Seller_Id
LEFT OUTER JOIN mst_Categories ON (mst_Categories.id = tbl_product.Category OR mst_Categories.id = tbl_product.SubCategory)
LEFT OUTER JOIN tbl_productspecification ON tbl_productspecification.ProductId = tbl_product.ProductId
LEFT OUTER JOIN mst_image ON mst_image.Product = tbl_product.ProductId
LEFT OUTER JOIN tbl_dealinterest ON tbl_dealinterest.ProductId = tbl_product.ProductId AND tbl_dealinterest.BuyerId = '$token'
WHERE tbl_product.Active='y'
AND tbl_product.StartTime <= '".date("Y-m-d H:i:s")."'
AND tbl_product.EndTime > '".date("Y-m-d")." 06:00:00'
".$subquery."
GROUP BY tbl_productspecification.ProductId";
Thanks for any suggestions.
SELECT ...
WHERE tbl_product.Active='y'
AND (tbl_dealinterest.Active <> 'n' OR tbl_dealinterest.Active IS NULL)
...
LEFT OUTER JOIN tbl_dealinterest ON (tbl_dealinterest.ProductId = tbl_product.ProductId
AND tbl_dealinterest.BuyerId = '$token'
AND tbl_dealinterest.Active<>'n')

Using INNER JOIN twice in the same query

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

Categories