I have a question.
This is my query:
DROP VIEW IF EXISTS Bedrijven_speci;
CREATE VIEW Bedrijven_speci as SELECT bedrijfgegevens.id,
bedrijfgegevens.subbranche_id ,
(6371 * acos(cos(radians(51.61162253395352)) * cos(radians(bedrijfgegevens.latitude)) * cos(radians(bedrijfgegevens.longitude) - radians(5.535746802487663)) + sin (radians(51.61162253395352)) * sin (radians(bedrijfgegevens.latitude))) ) AS distance
FROM bedrijfgegevens
INNER JOIN subbranches on subbranches.id = bedrijfgegevens.subbranche_id
INNER JOIN branches on branches.id = subbranches.branche_id
INNER JOIN bedrijfgegevens_specialiteiten on bedrijfgegevens_specialiteiten.bedrijfgegevens_id = bedrijfgegevens.id
INNER JOIN specialiteiten on specialiteiten.id = bedrijfgegevens_specialiteiten.specialiteiten_id
WHERE branches.naam = "Fotografie" AND subbranches.naam = "fotografen"
GROUP BY bedrijfgegevens.id HAVING distance < 10;
SELECT branches.naam as branche_naam, subbranches.naam as subbranche_naam, specialiteiten.naam as specialiteiten_naam
FROM Bedrijven_speci
INNER JOIN subbranches on subbranches.id = Bedrijven_speci.subbranche_id
INNER JOIN branches on branches.id = subbranches.branche_id
INNER JOIN bedrijfgegevens_specialiteiten on bedrijfgegevens_specialiteiten.bedrijfgegevens_id = Bedrijven_speci.id
INNER JOIN specialiteiten on specialiteiten.id = bedrijfgegevens_specialiteiten.specialiteiten_id
$query;
$sth = $pdo->prepare($query);
$sth->execute();
$fetch = $sth->fetchAll();
It works when I execute the query in Phpmyadmin. But when I execute in with PDO it returns an empty array. Can someone help me?
Just execute the statements one after another.
If required start a transaction before executing the the statements via PDO::beginTransaction
<?php
$queries = array(
'DROP VIEW IF EXISTS Bedrijven_speci',
'CREATE VIEW Bedrijven_speci as
SELECT
bedrijfgegevens.id,bedrijfgegevens.subbranche_id,
(6371 * acos(cos(radians(51.61162253395352)) * cos(radians(bedrijfgegevens.latitude)) * cos(radians(bedrijfgegevens.longitude) - radians(5.535746802487663)) + sin (radians(51.61162253395352)) * sin (radians(bedrijfgegevens.latitude))) ) AS distance
FROM bedrijfgegevens
INNER JOIN subbranches on subbranches.id = bedrijfgegevens.subbranche_id
INNER JOIN branches on branches.id = subbranches.branche_id
INNER JOIN bedrijfgegevens_specialiteiten on bedrijfgegevens_specialiteiten.bedrijfgegevens_id = bedrijfgegevens.id
INNER JOIN specialiteiten on specialiteiten.id = bedrijfgegevens_specialiteiten.specialiteiten_id
WHERE branches.naam = "Fotografie" AND subbranches.naam = "fotografen"
GROUP BY bedrijfgegevens.id HAVING distance < 10',
'SELECT branches.naam as branche_naam, subbranches.naam as subbranche_naam, specialiteiten.naam as specialiteiten_naam
FROM Bedrijven_speci
INNER JOIN subbranches on subbranches.id = Bedrijven_speci.subbranche_id
INNER JOIN branches on branches.id = subbranches.branche_id
INNER JOIN bedrijfgegevens_specialiteiten on bedrijfgegevens_specialiteiten.bedrijfgegevens_id = Bedrijven_speci.id
INNER JOIN specialiteiten on specialiteiten.id = bedrijfgegevens_specialiteiten.specialiteiten_id'
);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->beginTransaction();
try {
foreach( $queries as $q ) {
$pdo->exec($q);
}
}
catch(Exception $ex) {
$pdo->rollBack();
throw $ex;
}
$pdo->commit();
Related
Description:
table1 - columns: f1,f2,f3,f4,f5
in these columns I have rom_number for temperature sensors
table2 - columns: act_temperature, rom_number
I need act_temperature from table2 for each rom_number stored in table1 (cols f1,f2,...)
I have code:
// main loop
$rows = $db->query("SELECT * FROM tabela1 WHERE active='on' ");
$row = $rows->fetchAll();
foreach ($row as $a) {
$f1 = $a['f1'];
$f2 = $a['f2'];
$f3 = $a['f3'];
$f4 = $a['f4'];
$f5 = $a['f5'];
}
How to change the code ?
You have to LEFT JOIN on table 2 for each columns f1,f2,...
$sql = "SELECT t1.*,
t2_1.act_temperature as tmp1,
t2_2.act_temperature as tmp2,
t2_3.act_temperature as tmp3,
t2_4.act_temperature as tmp4,
t2_5.act_temperature as tmp4
FROM tabela1 t1
LEFT JOIN tabela2 t2_1 ON t2_1.rom_number = t1.f1
LEFT JOIN tabela2 t2_2 ON t2_2.rom_number = t1.f2
LEFT JOIN tabela2 t2_3 ON t2_3.rom_number = t1.f3
LEFT JOIN tabela2 t2_4 ON t2_4.rom_number = t1.f4
LEFT JOIN tabela2 t2_5 ON t2_5.rom_number = t1.f5
WHERE active='on' ";
Then, $a should have following keys : f1,f2,...,f5,tmp1,tmp2,...,tmp5.
Please help me to check my query. I have search a lot and I have'nt try to select 3 tables before.
I think I got it right but I dont know why there's nothing happen.
public function delSection($delete_id)
{
$stmt = $this->conn->prepare("SELECT * FROM tbl_section
JOIN tbl_login ON (tbl_login.sec_id = tbl_section.sec_id)
JOIN tbl_content ON (tbl_content.sec_id = tbl_section.sec_id)
WHERE tbl_section.sec_id=:del_id");
$stmt->execute(array(":del_id"=>$delete_id));
while($linkRow=$stmt->fetch(PDO::FETCH_ASSOC))
{
unlink(__DIR__."/Admin/cover_images/".$linkRow['sec_cover']);
unlink(__DIR__."/Admin/Files/".$linkRow['sec_id']."/".$linkRow['file_name']);
rmdir(__DIR__."/Admin/Files/".$linkRow['sec_id']);
}
$stmt2 = $this->conn->prepare("DELETE tbl_section, tbl_login, tbl_content FROM tbl_section
JOIN tbl_login ON (tbl_login.sec_id = tbl_section.sec_id)
JOIN tbl_content ON (tbl_content.sec_id = tbl_section.sec_id)
WHERE tbl_section.sec_id=:del_id");
$stmt2->bindparam(":del_id",$delete_id);
$stmt2->execute();
return true;
}
What I am trying to do is to select * from 3 tables and fetch their data with fk sec_id
here's the manual running of query
link:
Code:
Done With LEFT OUTER JOIN QUERY
$stmt = $this->conn->prepare("SELECT * FROM tbl_section
LEFT OUTER JOIN tbl_login ON (tbl_login.sec_id = tbl_section.sec_id)
LEFT OUTER JOIN tbl_content ON (tbl_content.sec_id = tbl_section.sec_id)
WHERE tbl_section.sec_id=:unlink_id");
Before you guys send me to something else because there is already something like this answered. I don't quite understand the examples given there.
function joinBestelling (){
$sql =( "SELECT
artikel.artikelCode,
factuurregel.aantal
FROM artikel
INNER JOIN factuurregel
ON artikel.artikelCode = factuurregel.artikelCode");
$stmt = $this->conn->prepare($sql);
$stmt->execute();
$records = $stmt->fetchAll();
return $records;
}
so i have two tables now artikel and factuurregel how do i add like 3 more tables with info in the join ?
Simply join the outher tables like the first one:
SELECT
artikel.artikelCode,
factuurregel.aantal
FROM artikel
INNER JOIN factuurregel
ON artikel.artikelCode = factuurregel.artikelCode
INNER JOIN table3 on .....
INNER JOIN table4 on .....
Hopefully that was your question
function joinBestelling (){
$sql =( "SELECT factuurregel.factuurNummer, factuurregel.artikelCode, factuurregel.aantal, factuurregel.prijs, factuur.factuurNummer, factuur.factuurDatum, factuur.klantCode, klant.klantCode, klant.voorLetter, klant.achterNaam, artikel.artikelCode, artikel.artikel, artikel.prijs FROM factuurregel JOIN factuur ON factuurregel.factuurNummer = factuur.factuurNummer JOIN klant ON factuur.klantCode = klant.klantCode JOIN artikel ON factuurregel.artikelCode = artikel.artikelCode");
$stmt = $this->conn->prepare($sql);
$stmt->execute();
$records = $stmt->fetchAll();
return $records;
}
I have php/mysql script running which is like this:
$sql = "select a.column1 from table1 a";
$query = mysql_query($sql);
while ($fec = mysql_fetch_assoc($query)) {
$sub1 = "select column1,column2 from subtable1 where id=" . $fec['a.column1'];
$subquery1 = mysql_query($sub1);
while ($subfec1 = mysql_fetch_assoc($subquery1)) {
//all data .....
}
$sub1 = "select column1,column2 from subtable2 where id=" . $fec['a.column1'];
$subquery2 = mysql_query($sub2);
while ($subfec2 = mysql_fetch_assoc($subquery2)) {
//all data .....
}
$sub2 = "select column1,column2 from subtable3 where id=" . $fec['a.column1'];
$subquery3 = mysql_query($sub3);
while ($subfec3 = mysql_fetch_assoc($subquery3)) {
//all data .....
}
}
Now I've many many many records in table1, subtable1, subtable2 and subtable3. And problem is that it takes around 7 hours to display the results. Moreover the CPU Usage is 100%
Please suggest the optimization tips to overcome this issue.
Use a single query to get all records
SELECT
a.column1,
s.column1,
s.column2,
s2.column1,
s2.column2,
s3.column1,
s3.column2
FROM table1 a
LEFT JOIN subtable1 s ON s.id = a.column1
LEFT JOIN subtable2 s2 ON s.id = a.column1
LEFT JOIN subtable3 s3 ON s.id = a.column1
Here
$sql="
SELECT
a.column1 acolumn1,
s.column1 scolumn1,
s.column2 scolumn2,
s2.column1 s2column1,
s2.column2 s2column2,
s3.column1 s3column1,
s3.column2 s3column2
FROM table1 a
LEFT JOIN subtable1 s ON s.id = a.column1
LEFT JOIN subtable2 s2 ON s.id = a.column1
LEFT JOIN subtable3 s3 ON s.id = a.column1";
$query=mysql_query($sql);
while ($fec=mysql_fetch_assoc($query)) {
// display any info here
}
Use aliases for accessing different table columns
I'm trying to use ResultSetMappingBuilder to get data from Native query.
$sql = "SELECT e.start_date FROM se_events e
LEFT JOIN se_event_tags tg ON e.id = tg.event_id
LEFT JOIN se_event_type t ON tg.event_type_id = t.id
WHERE t.id = :id";
I have no idea how to build ResultSetMappingBuilder.
public function createResultSetMapping() {
$rsm = new \Doctrine\ORM\Query\ResultSetMappingBuilder($this->getEntityManager());
$rsm->addRootEntityFromClassMetadata('Event', 'e');
return $rsm;
}
Thanks for help in advance.
So I've used row SQL:
$sql = "SELECT e.start_date FROM se_events e
LEFT JOIN se_event_tags tg ON e.id = tg.event_id
LEFT JOIN se_event_type t ON tg.event_type_id = t.id
WHERE t.id = $id";
$stmt = $this->getEntityManager()->getConnection()->prepare($sql);
$stmt->execute();
return $stmt->fetchAll();