Hi how can i avoid queries like these?
sample query:
$sql = DB::getInstance()->prepare("SELECT tb_id1 FROM table1 WHERE duedate < ?");
$sql->execute(array($currentdate));
if($sql->rowCount()){
while($row = $sql->fetch(PDO::FETCH_ASSOC)){
$sql2 = DB::getInstance()->prepare("UPDATE table2 SET isOverdue = 1 WHERE tb_id2 = ?");
$sql2->execute(array($row["tb_id1"]));
}
}
You can use update with join and thus by not using any loop in PHP
update table2 t2
join table1 t1 on t1.tb_id1 = t2.tb_id2
set t2.isOverdue = 1
where
t1.duedate < ?
Related
i am trying to update two tables with one query using inner join but it's not updating or neither showing any error. here is the code
$id_prod = 2;
$id_cust = 2;
$sql5 = "UPDATE `customer`
INNER JOIN `products` ON products.cust_id=customer.id
SET prod_name = 'CAKE' AND name = 'Hassan'
WHERE id='$id_cust' AND id='id_prod' ";
$query5 = $conn->prepare($sql5);
$query5->execute(array($id_cust, $id_prod));
Maybe the column to be set is misnamed, if you prefix the column name with the table name, is that better ?
Like this :
$id_prod = 2;
$id_cust = 2;
$sql5 = "UPDATE `customer`
INNER JOIN `products` ON products.cust_id=customer.id
SET __tableName__.prod_name = 'CAKE' AND __tableName__.name = 'Hassan'
WHERE __tableName__.id='$id_cust' AND __tableName__.id='id_prod' ";
$query5 = $conn->prepare($sql5);
$query5->execute(array($id_cust, $id_prod));
I have this code in PHP:
$sql = '
SELECT t1.name, t2.code, COUNT(t2.code) as cnt
FROM t1
LEFT JOIN t2
ON t2.code = t1.code2
WHERE (t2.input_date BETWEEN "2015-04-01" AND "2015-04-29")
AND t2.serial = 5
GROUP BY t2.code HAVING (t2.code>0)';
$Chartdata= mysql_query($sql) or die(mysql_error());
$arr = array();
while ($row = mysql_fetch_assoc($Chartdata)) {
$arr[] = $row;
}
echo json_encode($arr);
This code shows a blank page on my browser. I tried to delete the last one "AND" and "GROUP BY...", it shows 1 value.
Try just:
$sql = '
SELECT t1.name, t2.code, COUNT(*) as cnt
FROM t1
LEFT JOIN t2
ON t2.code = t1.code2
WHERE (t2.input_date BETWEEN "2015-04-01" AND "2015-04-29")
AND t2.serial = 5
AND t2.code>0
GROUP BY t2.code';
I have to write the following query in zend-framework2
select count(*) from
(
select * from table1
UNION
select * from table2
)
as a
where col1 = condition1 and col2 = condition2;
I have done union of two tables using -
$select1 = new Select('table1');
$select2 = new Select("table2");
$select1->combine($select2);
I don't know how to give an alias after doing the union of two tables and how to get the count of data.
After $select1->combine($select2); -
$sql = new Sql($this->tableGateway->adapter);
$select_string = $sql->getSqlStringForSqlObject($select1);
$sql_string = 'SELECT * FROM (' . $select_string . ') AS select_union WHERE col1 = condition1 and col2 = condition2';
$statement = $this->tableGateway->adapter->createStatement($sql_string);
$resultSet = $statement->execute();
$total_records = count($resultSet);
$resultSet gives data.
$total_records gives total no. of records.
Ok, so this may sound a little strange and maybe over complicated. Here is the situation. I 2 sets of 3 queries. I will try to make a simple example to explain exactly what I am trying to do:
Queries:
//First set of queries
$query1 = "SELECT Name, Date FROM Table1";
$query2 = "SELECT Type, Place, Location FROM Table2";
$query3 = "SELECT One FROM Table3";
//Second set of queries
$query4 = "SELECT Name, Date FROM Table1 WHERE ID=1";
$query5 = "SELECT Type, Place, Location FROM Table2 WHERE ID=1";
$query6 = "SELECT One FROM Table3 WHERE ID=1";
You just have to trust me when I tell you that I CANNOT combine these two sets of queries. these are over simplified select statements to get the concept of what I am trying to do.
So here is my php code:
//Set 1
$data1 = mysql_query($query1) or die(mysql_error());
$data2 = mysql_query($query2) or die(mysql_error());
$data3 = mysql_query($query3) or die(mysql_error());
while ($line1 = mysql_fetch_array($data1, MYSQL_ASSOC) &&
$line2 = mysql_fetch_array($data2, MYSQL_ASSOC)) {
while ($line3 = mysql_fetch_array($data3, MYSQL_ASSOC)) {
//COMBINE $line1, line2, line3 into a single $lineSet1 -- HOW DO I DO THIS?
}
}
//Set 2
$data4 = mysql_query($query4) or die(mysql_error());
$data5 = mysql_query($query5) or die(mysql_error());
$data6 = mysql_query($query6) or die(mysql_error());
while ($line4 = mysql_fetch_array($data4, MYSQL_ASSOC) &&
$line5 = mysql_fetch_array($data5, MYSQL_ASSOC)) {
while ($line6 = mysql_fetch_array($data6, MYSQL_ASSOC)) {
//COMBINE $line4, line5, line6 into a single $lineSet2 -- HOW DO I DO THIS?
}
}
//Append $lineset1 and $lineset2 so I have 1 resultset $results
$result = array_merge($lineSet1, $lineSet2);
//So now I can pass this $result array into my array2csv function that takes a multidimensional array:
function array2csv(array &$array)
{
if (count($array) == 0) {
return null;
}
ob_start();
$df = fopen("exportedLeads{$_SESSION['id']}.csv", 'w');
fputcsv($df, array_keys(reset($array)));
foreach ($array as $row) {
fputcsv($df, $row);
}
fclose($df);
return ob_get_clean();
}
I know this seems really complicated, but I am pretty confused and not that good at php. Any help would be appreciated. Thanks!
TABLE1:
ID | Name | Date
TABLE2:
ID | Table1_ID | Type | Place | Location
TABLE3:
ID | Table1_ID | One
EDIT: I have been reading into JOIN statements. Is this possible a case for that?
You can resume both of your sets into a single query like using JOIN assuming your ID's match.
First set into 1 query:
SELECT t1.Name, t1.Date, t2.Type, t2.Place, t3.One FROM Table1 t1
JOIN Table2 t2
ON t2.Table1_ID = t1.ID
JOIN Table3 t3
ON t3.Table1_ID = t1.ID
Second set into 1 query:
SELECT t1.Name, t1.Date, t2.Type, t2.Place, T2.Location, t3.One
FROM Table1 t1
JOIN Table2 t2
ON t2.Table1_ID = t1.ID
JOIN Table3 t3
ON t3.Table1_ID = t1.ID
WHERE t1.ID = 1
Assuming your three queries all return the SAME number of rows, you can abuse the assignment operator:
while(($row1 = mysql_fetch($result1))
&& ($row2 = mysql_fetch($result2))
&& ($row3 = mysql_fetch($result3))) {
to fetch from all three result sets in parallel. The downside is that if your result sets are different lengths, you'll only fetch as many records as there are in the SHORTEST result set. e.g. as soon as any of those fetch calls returns false, the entire while aborts, even if there's still data in the other two results.
I was thinking of accomplishing the following as a PHP multi_query. But I'm trying to figure out how to pass the column value from the select query to the insert and update queries.
$query = "SELECT tbl_links.link, link_id
FROM tbl_links
INNER JOIN tbl_items ON tbl_links.item_id = tbl_items.item_id
WHERE tbl_items.item_name like '".$items_name[$counter]."'
AND NOT EXISTS (
select link_id
from tbl_clickedlinks
where tbl_clickedlinks.link_id = tbl_links.link_id
AND tbl_clickedlinks.cust_id = '$items_custID[$counter]'
)
limit 0, 1;" ;
$query .= "INSERT INTO tbl_claimedlinks (cust_id, link_id, claim_time) VALUES ('$items_custID', $row['link_id'], NOW()) ;";
$query .= "UPDATE tbl_links SET click_count = click_count+1 where link_id = '$linkID' ;";*/
Problem is, I'm not sure how to pass the link_id value to the other queries. So I'm thinking I might have to rearrange the queries into one, but again, I'm just not sure how to pull that off.
Anyone got any suggestions?
You need to execute select query 1st then use its output to execute 2nd & 3rd query.
$query = "SELECT tbl_links.link, link_id
FROM tbl_links
INNER JOIN tbl_items ON tbl_links.item_id = tbl_items.item_id
WHERE tbl_items.item_name like '".$items_name[$counter]."'
AND NOT EXISTS (
select link_id
from tbl_clickedlinks
where tbl_clickedlinks.link_id = tbl_links.link_id
AND tbl_clickedlinks.cust_id = '$items_custID[$counter]'
)
limit 0, 1;" ;
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$query2 = "INSERT INTO tbl_claimedlinks (cust_id, link_id, claim_time) VALUES ('$items_custID', $row['link_id'], NOW()) ;";
$query3 = "UPDATE tbl_links SET click_count = click_count+1 where link_id = '$linkID' ;";*/
mysql_query($query2);
mysql_query($query3);
}