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 .....
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.
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 />';
}
function sum($nomId){
$sql = "SELECT SUM(nomDetCantidad) FROM table2 where Id = $nomId";
$Resultado=$this->ProcesaSQLQueryList($sql);
if($Resultado>0){
foreach($Resultado as $key => $valor){
$cantidadTotal = $valor[0];
}
}
if($Resultado=='null'){$cantidadTotal=0;}
$sql = "UPDATE table1 SET nomCantidadTotal=$cantidadTotal,nomActualizado = NOW() WHERE nomId= $nomId";
return $this->ProcesaSQLQueryUpdate($sql);
and this is the function I call
function ProcesaSQLQueryList($SQLQuery){
$row= array();
if(!$this->link_mysql) $this->link_mysql = conectarManager();
if($res = mysql_query($SQLQuery,$this->link_mysql)){
while($r = mysql_fetch_array($res)){
$row[] = $r;
}
mysql_free_result($res);
return $row;
}else{
$this->last_error = $SQLQuery . " - " . mysql_error();
return -1;
}
}
So I sum up whatever I have in a field table 2 with the foreign key of table 1 then my result gets updated to the table 1 field but if I delete the fields in table 2 result wont get updated to 0 any suggestions I'm pretty sure this is an easy one but can't seem to find it
$sql = "SELECT SUM(nomDetCantidad) FROM table2 where nominaId = $nomId";
$Resultado = $this->ProcesaSQLQueryList($sql);
if ($Resultado > 0) {
foreach ($Resultado as $key => $valor) {
$cantidadTotal = $valor[0];
}
}
if ($cantidadTotal == '') {
$cantidadTotal=0.00;
}
$sql = "UPDATE table1 SET nomCantidadTotal=$cantidadTotal,nomActualizado = NOW() WHERE nomId = $nomId";
return $this->ProcesaSQLQueryUpdate($sql);
There! It was sending me a blank variable xD but fixed and there are more suitable ways to do it, I agree with Spencer but if a client wants a red swing he has to get a red swing you know? :) thanks for the help guys!
If you need a zero returned when there are no "matching" rows in table2, then wrap the return expression in an IFNULL function.
SELECT IFNULL(SUM(nomDetCantidad),0) FROM table2 ...
You could significantly reduce the amount of code you have, reduce the number of roundtrips to the database, and improve performance by doing all this work in a single UPDATE statement. You can use either a correlated subquery or an OUTER JOIN:
-- using a correleated subquery
UPDATE table t1
SET t1.nomActualizado = NOW()
, t1.nomCantidadTotal =
( SELECT IFNULL(SUM(t2.nomDetCantidad),0) AS nomCantidadTotal
FROM table2 t2
WHERE t2.Id = t1.nomID
)
WHERE t1.nomId = $nomId
-- using an OUTER JOIN
UPDATE table1 t1
LEFT
JOIN (SELECT t2.Id, SUM(t2.nomDetCantidad) AS nomCantidadTotal
FROM table2 t2
WHERE t2.Id = $nomId
GROUP BY t2.Id
) s
ON s.Id = t1.nomID
SET t1.nomCantidadTotal = IFNULL(s.nomCantidadTotal,0)
, t1.nomActualizado = NOW()
WHERE t1.nomID = $nomId
It's not at all clear why you need to store this total on table1, when you could derive it from table2 whenever you need it, without storing that value on table1 at all...
SELECT t1.nomId
, IFNULL((SELECT SUM(t2.nomDetCantidad) AS nomCantidadTotal
FROM table2 t2
WHERE t2.Id = t1.nomID
),0) AS nomCantidadTotal
FROM table1 t1
WHERE t1.nomId = $nomId
Is it possible to strip away columns from the response I get in a query where I join 3 tables and need more or less all columns for the query itself so that some columns aren't visible in the response?
This is the query I have:
$sth = mysql_query("
SELECT
tbl_subApp2Tag.*,
tbl_subApp.*,
tbl_tag.*
ISNULL(tbl_userDeviceNOTTag.userDevice_id) AS selected
FROM tbl_subApp2Tag
LEFT JOIN tbl_subApp
ON tbl_subApp.id = tbl_subApp2Tag.subApp_id
AND tbl_subApp.subApp_id = '".$sub."'
LEFT JOIN tbl_tag
ON tbl_tag.id = tbl_subApp2Tag.tag_id
LEFT JOIN tbl_userDeviceNOTTag
ON tbl_userDeviceNOTTag.tag_id = tbl_tag.id
AND tbl_userDeviceNOTTag.userDevice_id = '".$user."'
WHERE tbl_subApp2Tag.subApp_id = tbl_subApp.id
ORDER BY tbl_tag.name ASC ");
if(!$sth) echo "Error in query: ".mysql_error();
while($r = mysql_fetch_assoc($sth)) {
$rows[] = $r;
}
You do not need to include columns in the result table, just because they are referenced elsewhere in the query. Just select the columns that you need.
I have the following query:
SELECT *
FROM scool
LEFT JOIN people as t1 ON t1.scool_fk=scool.id AND t1.lang_gk='en'
LEFT JOIN people as t2 ON t2.scool_fk=scool.id AND t2.lang_gk='fr'
...
(this is nonsense query, only for example)
With SELECT * it query returns french values
With SELECT t1.* it query returns english values
The only possible solution i know is
SELECT t1.name as name_en, t2.name as name_fr
This don't like me because i can't automatize the selects in my program
Is possible to be return all values from SELECT with tablename.columnname or other similar solution, for get values in php?
Use mysql_fetch_field and properties table and name:
<?php
mysql_connect("", "", "");
mysql_select_db("test");
$res = mysql_query("SELECT * FROM t_ai a1 CROSS JOIN t_ai a2 LIMIT 1") or die(mysql_error());
$names = array();
for ($i = 0; $i < mysql_num_fields($res); $i++) {
$meta = mysql_fetch_field($res, $i);
array_push($names, "$meta->table.$meta->name");
}
print implode($names, "\t") . "\n";
?>
$ php phptest.php
a1.id a2.id