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.
Related
i have this select:
SELECT
a.`cod_oportunidade`,
b.nome nome_cliente,
c.descricao estado,
d.descricao cidade,
e.nome nome_funcionario_criou,
f.nome nome_funcionario_resp,
i.`descricao`,
h.`valor`
FROM
oportunidades_clientes a
LEFT OUTER JOIN empresas_clientes b
ON b.cod_cliente = a.cod_cliente
LEFT OUTER JOIN sistema_estados c
ON c.cod_estado = b.cod_estado
LEFT OUTER JOIN sistema_cidades d
ON d.cod_cidade = b.cod_cidade
LEFT OUTER JOIN empresas_funcionario e
ON e.cod_funcionario = a.cod_funcionario_criou
LEFT OUTER JOIN empresas_funcionario f
ON f.cod_funcionario = a.cod_funcionario_resp
JOIN formulario_valor h
ON h.`cod_oportunidade` = a.`cod_oportunidade`
JOIN formulario_campo i
ON i.`cod_campo` = h.`cod_campo`
WHERE 1 = 1
AND a.`cod_oportunidade` = 3
The result is therefore:
My question is... Need to inves need to stay several lines that the result is only one line. The data in the column "descricao" must be as columns ...
The implode() function will separate an array (columns from a database) with whatever character you prefer, in your case a pipe:
$mysqli = mysqli_connect("host","user","pass","db");
$result = mysqli_query($mysqli, "SELECT ...");
while ($row = mysqli_fetch_array($result)) {
echo implode(" | ", $row) . '<br />';
}
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 have a query like this:
$query = "SELECT a.sender_id,
a.recipient_id,
a.form_id, due_date,
a.completed,
f.name,
p.priority,
u.first_name,
u.last_name,
SUM(a.completed) as completed_sum
FROM form_assignments a
JOIN forms f ON (form_id = f.id)
JOIN users u ON (sender_id = u.id)
JOIN priorities p ON (priority_id = p.id)
WHERE recipient_id = '{$_SESSION['user_id']}'
ORDER BY due_date ASC";
And a while loop like this:
$assignment_count = (mysqli_num_rows($result));
$assignments_row = array();
while ($row = mysqli_fetch_array($result)) {
$sender = $row['first_name'] . ' ' . $row['last_name'];
$form_id = $row['form_id'];
$form_name = $row['name'];
$priority = $row['priority'];
$due_date = date('m/d/Y', strtotime($row['due_date']));
$completed = $row['completed'];
$not_done = $assignment_count - $row['completed_sum'];
}
And it's only returning one row. It seems my SUM(a.completed) as completed_sum is causing the issues because the query worked fine before I added it, but I want to add up all the values in completed to use in my $not_done variable.
Can anyone help clarify what I'm doing wrong?
When you use an aggregate function like SUM, all the results will be aggregated into one row unless you use a GROUP BY clause to segregate them. But it looks to me like you don't need a SUM in the first place. Your loop is subtracting this value from a total, so you just need the value from each row -- when you subtract them all you'll have subtracted the total. So just select a.completed rather than SUM(a.completed).
For $not_done, you need to initialize it before the loop:
$not_done = $assignment_count;
Then during the loop you should do a running subtraction:
$not_done -= $row['completed'];
Try this query :
SELECT
a.sender_id,
a.recipient_id,
a.form_id,
a.due_date,
a.completed,
f.name,
p.priority,
u.first_name,
u.last_name,
b.completed as completed_sum
FROM
form_assignments AS a
LEFT JOIN (
SELECT form_id,SUM(completed) FROM form_assignments GROUP BY form_id
) AS b ON (a.form_id = b.form_id)
LEFT JOIN forms AS f ON (form_id = f.id)
LEFT JOIN users AS u ON (sender_id = u.id)
LEFT JOIN priorities AS p ON (priority_id = p.id)
WHERE
recipient_id = '{$_SESSION['user_id']}'
ORDER BY due_date ASC
I have a simple JOIN query:
$lstCheck = $dbWHMCS->query('SELECT * FROM tblmonitorports mp
INNER JOIN tblmonitorhosts h ON h.hst_id = port_mon
INNER JOIN tblhosting ho ON ho.id = h.hst_serverid
INNER JOIN tblclients cl ON cl.id = ho.userid');
while ($data = $lstCheck->fetch())
{
$serveridx = $data['ho.id'];
$clientid = $data['cl.id'];
}
My problem is that I have an "id" column in both the tblhosting and tblclients tables, so my variables both have the same id. I tried to set it using an alias in the example above (ho. and cl.) but it doesn't work. How can I do this in the example above?
Thank you!
How about this? (a bit rusty on php details, but this should do it):
$lstCheck = $dbWHMCS->query('SELECT ho.id hid, cl.id cid FROM tblmonitorports mp
INNER JOIN tblmonitorhosts h ON h.hst_id = port_mon
INNER JOIN tblhosting ho ON ho.id = h.hst_serverid
INNER JOIN tblclients cl ON cl.id = ho.userid');
while ($data = $lstCheck->fetch())
{
$serveridx = $data['hid'];
$clientid = $data['cid'];
}
You are selecting the records, with a wild card *, so you can't select the fields like that.
As per your query h.hst_serverid & ho.userid have the exact same value as you want. SO simply do this
while ($data = $lstCheck->fetch())
{
$serveridx = $data['hst_serverid'];
$clientid = $data['userid'];
}
However, selecting specific rows might sound better too
$lstCheck = $dbWHMCS->query('SELECT ho.id hid, cl.id cid, ....');
while ($data = $lstCheck->fetch())
{
$serveridx = $data['hid'];
$clientid = $data['cid'];
}
I'm only receiving data from the first table social_members and not the two other tables _meminfo and _memtext. both tables have data in them with the same m_id value that is in _meminfo and _memtext.
$res = mysql_query("SELECT sm.*, DATE_FORMAT(sm.m_lld,'%m/%d/%y')
AS m_lld_formatted
FROM social_members sm
JOIN social_meminfo smi ON (sm.m_id = smi.m_id)
LEFT OUTER JOIN social_memtext smt ON (sm.m_id = smt.m_id)
WHERE sm.m_user = '".mysql_real_escape_string($en['user'])."'");
if (#mysql_num_rows($res) == 0) call404();
$line = #mysql_fetch_assoc($res);
foreach ($line as $key => $value) {
$en['m'.$key] = str_replace("\n",'<br/>',stripslashes($value));
}
echo '<pre>'; print_r($line); echo '</pre>';
echo $en['mm_pos']; // test from social_meminfo
in your query you explicitly say that you only want data from the social_members table.
SELECT sm.* ....
has that effect (sm.* = all the columns from the table aliased as sm). if you want all the columns generated by your query, then you should instead do
SELECT * ....
You need to mention social_meminfo and social_memtext columns in your SELECT clause.
try this
SELECT * from .....