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;
Related
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);
Please help find duplicate entries in multiple columns of same row in MySQL:
If you want to find the records that have duplicates in columns you can use this query:
SELECT T1.* FROM tbl T1
JOIN
(SELECT id
FROM (
SELECT id, sample1 AS n from tbl
UNION ALL
SELECT id, sample2 AS n from tbl
UNION ALL
SELECT id, sample3 AS n from tbl
) AS X
GROUP BY id, n
HAVING COUNT(*) > 1
) T2
ON T1.id = T2.id;
You can also test it Here
I am not sure with MySql,
But in PHP below example will use.
Example
$query = "select * from table_name";
$result = mysqli_query($query);
while ($row = mysqli_fetch_array($result)) {
if ($row['sample1'] == $row['sample2']) {
// This row duplicate
}
else {
// This row not duplicate
}
}
SELECT Sample1, COUNT(*) C FROM tablename GROUP BY Sample1 HAVING C > 1;
Finding duplicate values in MySQL
MySQL select records for duplicates using multiple columns
I have five different SQL queries that I would like to return back the number of rows returned in a list. Basically I have a MySQL database and would like to show some statistics on how many records we have and how many of them have had information added. Examples of the queries are...
SELECT * FROM `ibf_ccs_custom_database_1`
SELECT * FROM `ibf_ccs_custom_database_2`
SELECT * FROM `ibf_ccs_custom_database_3`
SELECT * FROM `ibf_ccs_custom_database_1` WHERE `field_30` <> ''
SELECT * FROM `ibf_ccs_custom_database_2` WHERE `field_60` <> ''
Which should return back the following output
Total Records Database 1: 12,548
Total Records Database 2: 9,835
Total Records Database 3: 5,916
Filled Out Records in Database 1: 567
Filled Out Records in Database 2: 681
Any help is much appreciated!
It would be much more efficient to have the database prepare a result containing just the count of rows, rather than preparing a complete set of ten thousand plus rows.
For example, to have the database return the results as shown in the question:
SELECT CONCAT('Total Records Database 1: ',FORMAT(COUNT(1),0) AS txt
FROM `ibf_ccs_custom_database_1`
UNION ALL
SELECT CONCAT('Total Records Database 2: ',FORMAT(COUNT(1),0)
FROM `ibf_ccs_custom_database_2`
UNION ALL
SELECT CONCAT('Total Records Database 3: ',FORMAT(COUNT(1),0)
FROM `ibf_ccs_custom_database_3`
UNION ALL
SELECT CONCAT('Filled Out Records in Database 1: ',FORMAT(COUNT(1),0)
FROM `ibf_ccs_custom_database_1` WHERE `field_30` <> ''
UNION ALL
SELECT CONCAT('Filled Out Records in Database 1: ',FORMAT(COUNT(1),0)
FROM `ibf_ccs_custom_database_2` WHERE `field_60` <> ''
If you want to handle the labeling and formatting in the application layer, you could just return the counts in a single row:
SELECT t1.cnt AS t1_cnt
, t2.cnt AS t2_cnt
, t3.cnt AS t3_cnt
, f1.cnt AS f1_cnt
, f2.cnt AS f2_cnt
FROM ( SELECT COUNT(1) AS cnt FROM `ibf_ccs_custom_database_1` ) t1
JOIN ( SELECT COUNT(1) AS cnt FROM `ibf_ccs_custom_database_2` ) t2
JOIN ( SELECT COUNT(1) AS cnt FROM `ibf_ccs_custom_database_3` ) t3
JOIN ( SELECT COUNT(1) AS cnt FROM `ibf_ccs_custom_database_1` WHERE `field_30` <> '' ) f1
JOIN ( SELECT COUNT(1) AS cnt FROM `ibf_ccs_custom_database_1` WHERE `field_60` <> '' ) f2
I'd just build an array with key => value pairs for your row counts. Something like this:
$db_queries = array();
$query = "SELECT * FROM ibf_ccs_custom_database_1";
$res = $mysqli->query($query);
$db_queries['q1'] = $res->num_rows;
$query = "SELECT * FROM ibf_ccs_custom_database_2";
$res = $mysqli->query($query);
$db_queries['q2'] = $res->num_rows;
$query = "SELECT * FROM ibf_ccs_custom_database_3";
$res = $mysqli->query($query);
$db_queries['q3'] = $res->num_rows;
$query = "SELECT * FROM ibf_ccs_custom_database_1 WHERE field_30 <> ''";
$res = $mysqli->query($query);
$db_queries['q4'] = $res->num_rows;
$query = "SELECT * FROM ibf_ccs_custom_database_2 WHERE field_60 <> ''";
$res = $mysqli->query($query);
$db_queries['q5'] = $res->num_rows;
foreach($db_queries as $k => $v){
echo $k, " contains ", $v, " rows.";
}
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
My two tables look like this:
TABLE1 TABLE2
+--------------------+ +--------------------+
|field1|field2|field3| and |field2|field4|field5|
+--------------------+ +--------------------+
I am already running a SELECT query for TABLE1, and assorting all of the data into variables:
$query = "SELECT * FROM TABLE1 WHERE field2 = 2";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
if((!is_bool($result) || $result) && $num_rows) {
while($row = mysql_fetch_array($result))
{
$field1 = $row['field1'];
$field2 = $row['field2'];
$field3 = $row['field3'];
}
}
What I want to do is get the data from 'field4' on TABLE2 and add it to my variables. I would want to get field4 WHERE field2 = 2
SELECT
t1.*,
t2.field4
FROM
TABLE1 AS t1,
TABLE2 AS t2
WHERE
t1.field2 = 2
AND
t1.field2 = t2.field2
You want to join the two table which can be done explicitly (using the JOIN operator or one of its variants) our implicitly (by SELECTing from multiple tables directly).
From your description, I suppose that you want to do the join where field2 in the two tables have the same value (2).
If TABLE2 will not always provide a valid join candidate but you still want the data from TABLE1, you should use a LEFT JOIN (giving NULL for field4 where nothing matched):
SELECT
t1.*,
t2.field4
FROM
TABLE1 AS t1
LEFT JOIN
TABLE2 AS t2
ON
t1.field2 = t2.field2
WHERE
t1.field2 = 2
http://en.wikipedia.org/wiki/Join_%28SQL%29
You need to use a JOIN:
SELECT TABLE1.*,TABLE2.field4 FROM TABLE1 JOIN TABLE2 ON TABLE1.field2=TABLE2.field2 WHERE TABLE1.field2=2;
hi try the following as ur query.
It is not preferred all the time to use * in the select query
SELECT T1.field1,T1.field2,T1.field3, T2.field4 FROM TABLE1 AS T1 INNER JOIN TABLE2 AS T2 ON T1.field2=T2.field2 WHERE field2 = 2