Multiple select statements in one query - php

I can do the following in 2 queries but want to make it simpler. Can this be combined in one query? If so how more efficient is it than doing two queries vs one?
query1: SELECT page_id, coupon_id from table_1 WHERE key = :key
query2: SELECT folder from table_2 WHERE page_id = table_1.page_id
For my final result I need to have a coupon_id from table_1, and a folder from table_2.
In query2 I need to use the page_id result from query1 to get the folder
Is there a simpler way to do this?

Use JOIN (LEFT, RIGHT or INNER is up to your needs):
SELECT
t1.page_id,
t1.coupon_id,
t2.folder
FROM
table_1 AS t1
LEFT JOIN table_2 AS t2 ON
t2.page_id = t1.page_id
WHERE
t1.key = :key

You will want to JOIN the tables on the page_id:
SELECT t1.page_id,
t1.coupon_id,
t2.folder
from table_1 t1
inner join table_2 t2
on t1.page_id = t2.page_id
WHERE key = :key
If you need help learning join syntax, here is a great visual explanation of joins.
I used an INNER JOIN which will return all rows that match between the two tables. If you want to return all rows from table_1
even if it doesn't have a matching row in table_2, then you would use a LEFT JOIN

SELECT
table_1.coupon_id AS coupon_id,
table_2.folder AS folder
FROM
table_1
INNER JOIN table_2 ON table_2.page_id = table_1.page_id
WHERE
table_1.key = :key

SELECT t1.page_id, t1.coupon_id, t2.folder
FROM table_1 t1 LEFT JOIN table_2 t2 ON (t1.page_id = t2.page_id)
WHERE t1.key = :key
This will be faster than two queries, how much depends on your data.

Try this please:
SELECT a.page_id, a.coupon_id, b.folder_id
from table_1 a
join table_2 b
ON a.page_id = b.page_id
WHERE a.key = :key
group by a.page_id
;

Related

How to Avoid SubQuery with same output in MySql

Good Day!
I already saw other post about avoiding subquery / using JOIN statement, but still I can't figure it out why my query is so very slow to execute the 9 result data. but when the result data is more than thousands the query execute 0.7k ms only.
My question is , how can I speed up this query execution, what will I remove/add, Or do I need to re-construct the query? how?
here's my query
SELECT a.ts, b.bc, b.rem, c.dept FROM table1 a
INNER JOIN (select doc, max(ID) from table1 Group By doc) d ON d.doc=a.doc AND d.ID=a.ID
INNER JOIN table2 c ON c.u_id=a.u_id
INNER JOIN table3 b ON b.id=a.doc
WHERE c.depart = 'deparment' AND b.end = 0
here is the Screen Shot of
EXPLAIN result
Updated SS for EXPLAIN result
I already set INDEX :
ALTER TABLE table3 ADD INDEX max_id (end,id,bc);
ALTER TABLE table1 ADD INDEX max_id (ID,doc,u_id,ts,rem);
ALTER TABLE table1 ADD INDEX m_id (doc,ID);
ALTER TABLE table2 ADD INDEX user_max (dept,u_id);
Try a correlated subquery:
SELECT a.ts, b.bc, b.rem, c.dept
FROM table1 a INNER JOIN
table2 c
ON c.u_id = a.u_id INNER JOIN
table3 b
ON b.id =a.doc
WHERE c.depart = 'deparment' AND b.end = 0 AND
d.id = (SELECT MAX(t1.id) FROM table1 t1 WHERE t1.doc = a.doc);
For the correlated subquery you want an index on table1(doc, id).

compare two tables with same column for similar values

I have two tables tableA and tableB. Both have two similar columns ID and B_ID.
I want to check whether both table have similar values. My code is:
$ac = $mysql->query("(SELECT ID,B_ID FROM tableA) INTERSECT (SELECT ID,B_ID FROM tableB)");
if($ac){
while($row = $ac->fetch_assoc()){
echo "ID ".$row["ID"]." B_ID".$row["B_ID"]."<br>";
}
}
But this doesn't give any result.
ps: tableA(ID,B_ID)
1->23
2->23
3->23
4->56
5->67
tableB(ID,B_ID)
3->23
8->26
11->27
12->66
here both table has 3->23 but 1->23 2->23 is not in tableB how can i figure that sort of records. same B_ID but different ID
If you have B.ID column is present in both table then use following JOIN query
SELECT a.ID, a.B_ID
FROM tableA AS a
JOIN tableB AS b ON (a.B_ID = b.B_ID AND a.ID = b.ID)
Use a join to get the data and just iterate over your results. Run the query and if there is any record that satisfies the query or not.
select t1.ID, t1.B_ID from tableA t1, tableB t2
where t1.ID = t2.ID
and t1.B_ID =t2.B_ID
Use an INNER JOIN and COUNT
SELECT COUNT(*) as cnt
FROM tableA INNER JOIN tableB
ON tableA.B_ID = tableB.B_ID AND tableA.ID = tableB.ID
now if cnt is greater than zero than common values exist, otherwise no
Please try with this query may be help full.
SELECT
tbla.ID,
tbla.B_ID
FROM
tablea AS tbla,
tableb AS tblb
WHERE
tbla.B_ID = tblb.B_ID

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

Join but return ALL records from Table

I have the following SQL query:
SELECT * FROM `table1` INNER JOIN `table2` ON table1.messageid=table2.messageid WHERE `venue_active` = 1
The above works fine but it only returns fields where both tables have a messageid field.
My problem is that I need it to return ALL fields from Table1 reguardless if it has a messageid match in table2 or not.
So, in other words I need ALL records to be returned from Table1 and all records from Table2 where there's a messageid that matches both.
How can I do this?
Use a LEFT JOIN rather
SELECT *
FROM `table1` LEFT JOIN
`table2` ON table1.messageid=table2.messageid
WHERE `venue_active` = 1
That said, it will only work if venue_active is also part of table1, and not table2.
Have a look at the different scenarios
SQL Fiddle DEMO
Use a LEFT join rather than INNER
For example:
SELECT * FROM `table1`
LEFT JOIN `table2` ON table1.messageid=table2.messageid
WHERE `venue_active` = 1
Either you need a LEFT JOIN instead, or
a FULL OUTER JOIN workaround for MySQL:
SELECT
a.*,
b.*
FROM
table1 a
LEFT JOIN
table2 b ON a.messageid = b.messageid
WHERE a.venue_active = 1
UNION
SELECT
a.*,
b.*
FROM
table1 a
RIGHT JOIN
table2 b ON a.messageid = b.messageid;
WHERE a.venue_active = 1

PHP SQL Query Fields from Another Table Join

I am trying to get a query using fields from 2 tables.
I need to query Table1 but only Table2 has the variable venue_location that I need to query.
Basically I need to count all records on Table1 where Table1.venue_location = $MyVariable.
Here is what I've put together but I believe I need to use Joins for this?
Table1
- venue_id
Table2
- venue_id,
- venue_location
SELECT * FROM Table1 WHERE table1.venue_id = table2.venue_id and table2.location = '$MyVariable'
How can I do a query for this?
Use the power of join table
SELECT * FROM Table1
JOIN Table2 USING(venue_id)
WHERE table2.location = '$MyVariable'
You can get back the count of rows with mysqli_num_rows() in PHP, or change le select by SELECT COUNT(*) AS nbRow FROM ... and check of value in nbRow column
You can join two tables on venue_id and then group it by venue_id where location is your $MyVariable.
Your final query will look like:
SELECT count(table2.venue_id)
FROM Table1
JOIN Table2 ON table1.venue_id = table2.venue_id
WHERE table2.location = '$MyVariable'
GROUP BY table2.venue_id
try this
SELECT Table1.venue_id, Table2.venue_location FROM Table1 INNER JOIN Table2
ON Table1.venue_id='$MyVariable';

Categories