How to get PostgreSQL ROW values with PHP - php

I have this query:
SELECT a.apartment_id, a.building_id, a.apartment_num, a.floor, at.app_type_desc_en AS app_type_desc,
(SELECT ROW(e.e_name, ot.otype_desc_en)
FROM TABLE_TENANTS t INNER JOIN TABLE_OWNERSHIP_TYPES ot ON ot.otype_id=t.ownership_type INNER JOIN TABLE_ENTITIES e ON
t.entity_id = e.entity_id
WHERE a.apartment_id = t.apartment_id AND t.building_id = a.building_id
ORDER BY t.ownership_type DESC LIMIT 1
) AS t_row
FROM TABLE_APARTMENTS a INNER JOIN TABLE_APPARTMENT_TYPES at ON at.app_type_id = a.apartment_type LEFT OUTER JOIN TABLE_TENANTS t ON a.building_id = t.building_id AND t.entity_id=1 AND t.status=true LEFT OUTER JOIN TABLE_COMMITTEE_DELEGATE cd ON
a.building_id = cd.building_id AND cd.entity_id=1 AND cd.status=true
WHERE a.building_id = 1 AND (t.entity_id=1 OR cd.entity_id=1)
ORDER BY a.apartment_num ASC
When I'm using PHP to read the result, I use:
while($ap = pg_fetch_array($_qry)){
}
and trying to read "e_name", which is part of the ROW(e.e_name), but I can't succeed.
Need your ideas ... please.

use pg_fetch_assoc, to retrieve data in asoc-array; But my advice, - use PDO to work with db in PHP.

Related

Having issues with this Mysql query

Hello i'm learning sql and i have some issues with joins(which i have problems understanding them)
I have this issue
#1066 - Not unique table/alias: 'tbl_respuestas'
what the query supposed to do, is count how many people(general,ignore user) has answer 'x', in 'y' question of 'z' survey
SELECT COUNT(*) FROM tbl_respuestas
INNER JOIN tbl_encuesta_usuario ON tbl_encuesta_usuario.user_id = user.id
INNER JOIN tbl_encuesta ON tbl_encuesta.id = tbl_encuesta_usuario.tbl_encuesta_id
INNER JOIN tbl_encuesta_has_tbl_preguntas ON tbl_encuesta_has_tbl_preguntas.tbl_encuesta_id = tbl_encuesta.id
INNER JOIN tbl_preguntas ON tbl_preguntas.id = tbl_encuesta_has_tbl_preguntas.tbl_preguntas_id
INNER JOIN tbl_preguntas_has_tbl_respuestas ON tbl_preguntas_has_tbl_respuestas.tbl_preguntas_id = tbl_preguntas.id
INNER JOIN tbl_respuestas ON tbl_respuestas.id = tbl_preguntas_has_tbl_respuestas.tbl_respuestas_id
WHERE tbl_respuestas.respuesta = 2
line SELECT COUNT(*) FROM tbl_respuestas
and line INNER JOIN tbl_respuestas
does not makes sense, hence the error.
Unless it is what you want then you need to give then different name/alias like below:
SELECT COUNT(*) FROM tbl_respuestas r
INNER JOIN tbl_respuestas r2
Also as a quick note you can rewrite the entire sql like below.
It is good practice to give your tables a name for shorter referencing and makes the sql look a little cleaner.
Also if both tables you are trying to join has the same column name then you can use the keyword USING instead of having to write that long line tbl_encuesta_usuario.user_id = user.id
Please be sure to put r and r2 in its prope place
SELECT COUNT(*) FROM tbl_respuestas r
INNER JOIN tbl_encuesta_usuario u USING user_id
INNER JOIN tbl_encuesta e ON e.id = u.tbl_encuesta_id
INNER JOIN tbl_encuesta_has_tbl_preguntas hp ON hp.tbl_encuesta_id = e.id
INNER JOIN tbl_preguntas p ON p.id = hp.tbl_preguntas_id
INNER JOIN tbl_preguntas_has_tbl_respuestas hr ON hr.tbl_preguntas_id = p.id
INNER JOIN tbl_respuestas r2 ON r2.id = hr.tbl_respuestas_id
WHERE r.respuesta = 2

Left join not working in my prepared statement (mysqli)

I can't get a left join to work in my prepared statement.
"SELECT DISTINCT(a.auto_id), m.merk, a.model, a.uitvoering, a.standaardtekst, a.prijs, a.prijs2, a.prijs3, a.handelsprijs, a.aanmaak, s.soort, z.prijs_id
/*,GROUP_CONCAT(DISTINCT(apc.NL) ORDER BY apc.NL ASC)*/
FROM autocom_new.auto_new a
INNER JOIN autocom_new.tbl_merken m
ON a.merk = m.merk_id
INNER JOIN autocom_new.tbl_soort s
ON a.soort = s.soort_id
INNER JOIN autocom_new.auto_zoekmachines z
ON a.auto_id = z.auto_id
/*
LEFT JOIN autocom_new.auto_accessoire acc
ON a.auto_id = acc.auto_id
LEFT JOIN autocom_new.tbl_autopricecode_new apc
ON acc.code_id = apc.code_id
*/
WHERE a.ac LIKE ? AND a.flag = ?"
The commented parts are the parts that aren't working.
I have no idea what I'm doing wrong.
EDIT
First of all I forgot that both tables have a column ac, so I've changed the where statement a bit. The left joins are working now, but the part in the select is still not working
So the problem was that I forgot a GROUP BY.
"SELECT DISTINCT(a.auto_id), m.merk, a.model, a.uitvoering, a.standaardtekst, a.prijs, a.prijs2, a.prijs3, a.handelsprijs, a.aanmaak, s.soort, z.prijs_id,
GROUP_CONCAT(DISTINCT(apc.NL) ORDER BY apc.NL ASC)
FROM autocom_new.auto_new a
INNER JOIN autocom_new.tbl_merken m
ON a.merk = m.merk_id
INNER JOIN autocom_new.tbl_soort s
ON a.soort = s.soort_id
INNER JOIN autocom_new.auto_zoekmachines z
ON a.auto_id = z.auto_id
LEFT JOIN autocom_new.auto_accessoire acc
ON a.auto_id = acc.auto_id
LEFT JOIN autocom_new.tbl_autopricecode_new apc
ON acc.code_id = apc.code_id
WHERE a.ac LIKE ? AND a.flag = ?
GROUP BY a.auto_id"

Using PHP in smarty template system to get SUM from database

Here is the code I'm currently using:
$result = mysql_query("
SELECT SUM(s.amount)
FROM tblaffiliatespending s
JOIN tblaffiliatesaccounts a
ON a.id=s.affaccid
JOIN tblhosting h
ON h.id = a.relid
JOIN tblproducts p
ON p.id = h.packageid
JOIN tblclients c
ON c.id = h.userid
WHERE affiliateid = $affiliateid
ORDER
BY clearingdate DESC;
");
$data = mysql_fetch_array($result);
$pendingcommissions = $data['?????????'];
$this->assign("pendingamount", $pendingcommissions);
What I'm not sure about is what to enter for ????????? on the third line. I've tried all of these things and none of them have worked:
$pendingcommissions = $data['SUM(tblaffiliatespending.amount)'];
$pendingcommissions = $data['SUM'];
$pendingcommissions = $data['tblaffiliatespending.amount'];
$pendingcommissions = $data['tblaffiliatespending'];
$pendingcommissions = $data['amount'];
Any ideas on what this needs to be changed to?
You need to give the alias for the sum of amount SUM(s.amount) total_amountso when you execute query and fetch the results from it you will have the sum of your amount column on total_amount index in resultant array and you can access it by $data['total_amount'];, also note using aggregate functions without grouping then will result in a single row not per group
SELECT SUM(s.amount) total_amount
FROM tblaffiliatespending s
JOIN tblaffiliatesaccounts a
ON a.id=s.affaccid
JOIN tblhosting h
ON h.id = a.relid
JOIN tblproducts p
ON p.id = h.packageid
JOIN tblclients c
ON c.id = h.userid
WHERE affiliateid = $affiliateid
ORDER
BY clearingdate DESC

mysql query optimization left join

I m using following mysql query to fetch some desired result but problem is that it is taking more time. Currently execution time for this query is 7456 ms which is unacceptable for my project, I want to optimize this query any idea?.
SELECT *, DATEDIFF(NOW(),ticketstatus_tbl.updation_date) AS problem_age,images_tbl.image_path
FROM ticketstatus_tbl
LEFT JOIN question_tbl ON ticketstatus_tbl.question_id = question_tbl.question_id
LEFT JOIN ticketing_tbl ON ticketstatus_tbl.related_ticket_id = ticketing_tbl.ticket_id
LEFT JOIN department_tbl ON question_tbl.question_dept = department_tbl.department_id
LEFT JOIN branch_tbl ON ticketstatus_tbl.branch_id = branch_tbl.id
LEFT JOIN images_tbl ON images_tbl.question_id = ticketstatus_tbl.question_id and images_tbl.branch_id = ticketstatus_tbl.branch_id
WHERE (ticketstatus_tbl.ticket_status NOT IN ('Close')
AND question_tbl.is_active_question = 1
AND ticketstatus_tbl.display_status = '1'
AND ticketstatus_tbl.flag_color = 'Yellow'
AND department_tbl.department_name = 'Admin')order by ticket_number ASC LIMIT 0 ,5
Thanks
First, you do not need all the left outer join, because your where clause is undoing most of them. My guess is all could be turned into inner joins, but at the minimum:
SELECT *, DATEDIFF(NOW(),ticketstatus_tbl.updation_date) AS problem_age,images_tbl.image_path
FROM ticketstatus_tbl
JOIN question_tbl ON ticketstatus_tbl.question_id = question_tbl.question_id
LEFT JOIN ticketing_tbl ON ticketstatus_tbl.related_ticket_id = ticketing_tbl.ticket_id
JOIN department_tbl ON question_tbl.question_dept = department_tbl.department_id
LEFT JOIN branch_tbl ON ticketstatus_tbl.branch_id = branch_tbl.id
LEFT JOIN images_tbl ON images_tbl.question_id = ticketstatus_tbl.question_id and images_tbl.branch_id = ticketstatus_tbl.branch_id
WHERE ticketstatus_tbl.ticket_status NOT IN ('Close')
AND question_tbl.is_active_question = 1
AND ticketstatus_tbl.display_status = '1'
AND ticketstatus_tbl.flag_color = 'Yellow'
AND department_tbl.department_name = 'Admin'
order by ticket_number ASC LIMIT 0 ,5;
Second, you are doing filtering onticketstatus_tbl. You should have a composite index on ticketstatus_tbl(display_status, flag_color, ticket_status, question_id). If you can, change the ticket_status not in ('Close') to an affirmative statement: ticket_status in (Open, 'In Progress', . . .). This makes it easier to use the index.
This is a start.

Join 3 tables using Doctrine_RawSql object

Is there any way to make this select:
SELECT *
FROM `sf_guard_user`
JOIN `friendship`
ON `friendship`.`user_id` = `sf_guard_user`.`id`
JOIN `circle`
ON `friendship`.`circle_id` = `circle`.`id`
WHERE `circle`.`id` = 1
ORDER BY `circle`.`id`
with a Doctrine_RawSql object without using foreign keys?
Why did you decide to use Doctrine_RawSql?
In this example, I'm using inner join:
SELECT sf.* FROM `sf_guard_user` sf
INNER JOIN `friendship` f on f.`user_id` = sf.`id`
INNER JOIN `circle` c on f.`circle_id` = c.`id`
WHERE c.`id` = 1
ORDER BY c.`id`

Categories