php sql count from 3 tables - php

I need to get a count from a table, based on data from 2 other tables.
This is my tables structure:
table1 (id, name)
table2 (id, a, b, c)
table3 (id, blah)
Can i do it all in one statement? Something like this:
SELECT count(*) from table3 WHERE table2.x=table1.name
The hard part is the x column is the name of 'table1.name'. So i dont actually know what x is when im running the statement.
This makes me think i'd have to run a statement to find the name of x before i run this one.
Or... maybe some JOIN?
CURRENT CODE WHICH I USE:
if ($rs[firearm] != "") {
$sql_result2 = mysql_query("SELECT * FROM db_firearms WHERE name='$rs[firearm]'", $db);
$rs2 = mysql_fetch_array($sql_result2);
$sql_result3 = mysql_query("SELECT * FROM items_firearms WHERE player='$id'", $db);
$rs3 = mysql_fetch_array($sql_result3);
if ($rs3[$rs2[shortname]] < 1) {
mysql_query("UPDATE players SET firearm = '' WHERE id ='$id'");
}
}

Yeah, sounds like joins will be necessary.
SELECT count(*) AS num_rows
FROM table3
LEFT JOIN table1.name ON table1.name = table3.name
INNER JOIN table2 ON table2.x = table1.name

Related

SQL Query Count across 2 tables

Here is what I have (i am including only the relevant cells to each table)
table1:
- user_id
table2:
- id
- manager_id
... and I have a PHP variable
$manager_id
So, what I am trying to do is get a COUNT(*) of how many records exist in table1, but the record should only be counted if table1.user_id (which is the table2.id) has a table2.manager_id == $manager_id
So, the only way I know how to do this would be to do something like this in PHP (which is wildly inefficient):
$query = "SELECT user_id FROM table1 WHERE {my where clause}";
// execute query and place into $item[] array (not shown for brevity)
foreach ( $item as $user_id ) {
$query = "SELECT manager_id FROM table2 WHERE id = '{$user_id}';
// execute query, place each item into $row
if ( $row['manager_id'] == $manager_id ) {
// tick up count by 1
}
I am fairly certain there is a way to do this purely in SQL, but I am at a loss.
You could try the below query:
select count(1) from table1 t1, table2 t2 where t1.user_id = t2.id and t2.manager_id = ?
you need to set your where clause to have this condition i.e.
SELECT Count(user_id) FROM table1 where
table1.user_id = table2.id
AND Convert('Varchar', table2.manager_id) = $manager_id
I assume your php variable $manager_id has a string
if it is not a number then there is no need to convert the sql column (table2.manager_id) to varchar.
You can do this with one SELECT statement:
SELECT COUNT(*)
FROM table1
WHERE user_id IN (
SELECT id
FROM table2
WHERE manager_id = <your-value-of-manager-id>
)
In case you need to use the data and want to count on PHP side you can use the following SELECT statement using a LEFT JOIN:
SELECT table1.*
FROM table1 LEFT JOIN table2 ON table1.user_id = table2.id
WHERE table2.manager_id = <your-value-of-manager-id>
To execute the SELECT statements you should use prepared statements (using PDO or mysqli).
example using the first query:
$sql = 'SELECT COUNT(*) FROM table1 WHERE user_id IN (SELECT id FROM table2 WHERE manager_id = ?)';
$sth = $dbh->prepare($sql);
$sth->execute([$manager_id]);
$rows = $sth->fetchAll();
if (count($rows) === 1) {
echo $rows[0][0];
}
example using the second query:
$sql = 'SELECT table1.* FROM table1 LEFT JOIN table2 ON table1.user_id = table2.id WHERE table2.manager_id = ?';
$sth = $dbh->prepare($sql);
$sth->execute([$manager_id]);
$rows = $sth->fetchAll();
echo count($rows);

How to search into table B if the result of searching into table A is zero?

I have three tables and I need to search in the first one, it there isn't any result, then search into second one and so on ..!
Here is my code:
// connecting to database
$stm1 = $this->dbh->prepare(" select * from table1 where col = :name; ");
$stm1->bindValue(":name", $name, PDO::PARAM_STR);
$stm1->execute();
$which_table = "table1";
// the result of searching into table1 is zero, then search into table2
if (!$stm1->rowCount()) {
$stm2 = $this->dbh->prepare(" select * from table2 where col = :name; ");
$stm2->bindValue(":name", $name, PDO::PARAM_STR);
$stm2->execute();
$which_table = "table2";
// the result of searching into table2 is zero, then search into table3
if (!$stm2->rowCount()) {
$stm3 = $this->dbh->prepare(" select * from table3 where col = :name; ");
$stm3->bindValue(":name", $name, PDO::PARAM_STR);
$stm3->execute();
$which_table = "table3";
// the result of searching into table3 is also zero
if (!$stm3->rowCount()) {
$which_table = 'none of them';
}
}
}
My code works as well, But it is slow, How can I optimize it and make it faster? As you see, there is three separated query and multiple if-statement .. How can I reduce them? Generally How can I improve that code? Can I do that using pure-sql?
If you code is slow, then you probably just need indexes:
create index idx_table1_col on table1(col);
create index idx_table2_col on table2(col);
create index idx_table3_col on table3(col);
With indexes, you can also phrase the query as a single statement, assuming the columns in the tables are the same:
select t1.*
from table1 t1
where t1.col = :name
union all
select t2.*
from table2 t2
where t2.col = :name and
not exists (select 1 from table1 t1 where t1.col = :name)
union all
select t3.*
from table3 t3
where t3.col = :name and
not exists (select 1 from table1 t1 where t1.col = :name) and
not exists (select 1 from table2 t2 where t2.col = :name);
This is a more complex query, but your code would only require a single query. And, with indexes, it should be very fast.
My code works as well, But it is slow, How can I optimize it and make it faster?
Add index for the column col
By the way, you may add limit 1 to your queries. It'll help you you have zillions of values in your tables to match

Pull MIN and MAX prices from database

I need to select all (*) the rows from table1, table2, table3.. but I need to select the MIN and MAX price from table 2 within this INNER JOIN. I've read up on how to do this, but how do I do this within an INNER JOIN, and how do I display it in a PHP variable.
Initial Problem: How do I display the min and max values once I pull them.. (e.g $Result['MinPrice'], $Result['MaxPrice']).
Here's my query:
$Query = mysql_query("
SELECT *
FROM table1
INNER JOIN table2 ON table1.UserID = table2.UserID
INNER JOIN tables3 ON table2.DeviceID = table3.DeviceID
WHERE table2.DeviceID = '$GetDeviceID'
");
Here is the tables structure:
table1 = usersinfo
UserID UserFirstName UserLastName UserDisplayName
1 John Doe John D.
table2 = listings
ListingID UserID DeviceID
11 1 2
table3 = devices
DeviceID
2
If you really want to do what you're asking in this way you can use the query that is displayed below. This does, however, return a lot of duplicate rows if you have multiple rows returned when querying. Try it and see if it works.
$Query = mysql_query("
SELECT table1.*, table2.*, table3.*,
MIN(table2.price) as minny,
MAX(table2.price) as maxxy
FROM table1
INNER JOIN table2 ON table1.UserID = table2.UserID
INNER JOIN tables3 ON table1.DeviceID = table3.DeviceID
WHERE table1.DeviceID = '$Something'
GROUP BY table2.ListingAskingPrice
");
Then get this value by doing $result['minny'] and $result['maxxy']

How we get unmatched values from tables in mysql databases

I have a problem in fetching data from mysql database tables.
I have two tables like table-1 and table-2 in below figure. How to get data from table-2 when pilotid is not equal to 1 in table-1.
I'm not sure, if I understand correctly, but this returns all rows of table-1, that do not have a matching entry in table-2. You can find the respective documentation of NOT EXISTS here.
SELECT *
FROM table-1 t1
WHERE NOT EXISTS( SELECT * FROM table-2 t2 WHERE t1.`Venueid` = t2.`Venueid` )
select a.venueid, a.name
from table2 a, table-1 b
where b.pilotid <> 1 and b.venueid = a.venueid;
SELECT Table_2.*
FROM Table_2
LEFT JOIN Table_1
ON Table_2.Venueid = Table_1.Venueid
WHERE Table_1.Venueid != 1
OR Table_1.Venueid NOT IN(1, 13, 15);
$sql = "select Venueid from Table1 where pilotid <> 1";
$data = mysql_query($sql);
while($row = mysql_fetch_assoc($data))
{
$ids[] = $row['Venueid'];
}
$sql2 = "select * from Table2 where venueid IN(".implode(',', $ids).")";
$data2 = mysql_query(sql2);
//$data2 contains the result-set resource;

php mysql join query

I need to pull data from 2 tables in my database. The data I pull from table 2 depends on the result of table 1.
I'm not amazing at all these JOINS and things, so if someone could just explain what kind of a JOIN i'd need here, and how it would look, i'd be grateful:
$sql_result = mysql_query("SELECT * FROM accounts WHERE id='$val'", $db);
$rs = mysql_fetch_array($sql_result); $name = $rs[name];
$sql_result2 = mysql_query("SELECT * FROM players WHERE name='$name'", $db);
$rs2 = mysql_fetch_array($sql_result2);
$sql="SELECT * FROM accounts JOIN players ON accounts.accounts_link_to_player_id_here=players.id WHERE accounts.id='$val'";
You can do something like this, depending on the structure of the table(s):
SELECT * FROM `accounts` INNER JOIN `players` USING (`name`) WHERE `accounts`.`id` = 'value';
SELECT * FROM accounts LEFT JOIN players USING (name) WHERE accounts.id = 'value';
You'll need a query that looks like this (this is known in SQL as a subselect):
"SELECT p.* FROM players p WHERE name IN ( SELECT n.name FROM accounts n WHERE n.id = '$val' )"

Categories