I have this query:
$query = '
SELECT * FROM "rdm_item"
LEFT JOIN "rdm_tag" ON "rdm_item"."aff_id" = "rdm_tag"."aff_type_id"
WHERE "rdm_item"."aff_publish" = 4
ORDER BY "rdm_item"."aff_id"
DESC LIMIT 0,12';
and tables:
rdm_item columns: aff_id/aff_publish/...
rdm_tag columns: aff_id/type_id/...
I could get results.
But for $result[0]['aff_id'] I expect rdm_item.aff_id but it return rdm_tag.aff_id
what's my problem?
You need to specify the columns you want from the select, prefixing them with the table name. For example:
$query = '
SELECT rdm_item.aff_id FROM "rdm_item"
LEFT JOIN "rdm_tag" ON "rdm_item"."aff_id" = "rdm_tag"."aff_type_id"
WHERE "rdm_item"."aff_publish" = 4
ORDER BY "rdm_item"."aff_id"
DESC LIMIT 0,12';
If you have duplicate column names in two tables (i.e. aff_id) use an identifier, e.g.
$query = '
SELECT rdm_item.aff_id as rdm_item_aff_id, rdm_tag.aff_id as rdm_tag_aff_id FROM "rdm_item"
LEFT JOIN "rdm_tag" ON "rdm_item"."aff_id" = "rdm_tag"."aff_type_id"
WHERE "rdm_item"."aff_publish" = 4
ORDER BY "rdm_item"."aff_id"
DESC LIMIT 0,12';
You can then refer to the correct column with something like result[0]['rdm_item_aff_id'].
Related
I am using the following query to get COUNT items from rows from the same table in LEFT JOIN.
This is the query:
SELECT
pac.id_sat as id_sat,
pac.nombre_contacto as nombre_contacto,
pac.centro_contacto as centro_contacto,
pac.tel_contacto as tel_contacto,
pac.horario_contacto as horario_contacto,
pac.email_contacto as email_contacto,
pac.num_factura as num_factura,
pac.fecha_factura as fecha_factura,
eq.nombre_equipo as modelo_equipo,
pac.num_serie as num_serie,
pac.tipo_incidencia as tipo_incidencia,
pac.cod_sat as cod_sat,
pac.estado as estado,
pac.clinica as clinica,
pac.fecha_sat as fecha_sat,
COUNT(medfotos.id_media_sat) as num_fotos,
COUNT(medvideos.id_media_sat) as num_videos
FROM tb_sat pac
LEFT JOIN tb_equipos eq ON pac.modelo_equipo = eq.id_equipo
LEFT JOIN tb_media_sat medfotos ON pac.cod_sat = medfotos.cod_sat AND medfotos.tipo = 1
LEFT JOIN tb_media_sat medvideos ON pac.cod_sat = medvideos.cod_sat AND medvideos.tipo = 2
WHERE pac.clinica = '".$idclinica."'
GROUP BY pac.id_sat
ORDER BY pac.fecha_sat DESC
My issue is that I am getting a wrong amount of COUNT items.
The real value for num_fotos should be 3 and for num_videos should be 2.
I am getting num_fotos = 6 and num_videos = 6.
EDIT
Table tb_sat
Table tb_media_sat
Sub-query will work better in your case like as follows:
SELECT pac.*, (SELECT COUNT(id_media_sat) FROM tb_media_sat WHERE cod_sat=pac.cod_sat AND tipo=1) AS num_fotos, (SELECT COUNT(id_media_sat) FROM tb_media_sat WHERE cod_sat=pac.cod_sat AND tipo=2) AS num_videos FROM tb_sat pac WHERE pac.clinica = '".$idclinica."' ORDER BY pac.fecha_sat DESC
Rest columns, please add yourself slowly slowly. I hope you will get correct output.
I would like to SUM the foglalas_mennyiseg field in the foglalas table.
With the code below, i get this error: Notice: Undefined index: foglalt
What am i doing wrong?
$sql =
"
SELECT
rendeles.rendeles_id,
rendeles.rendeles_gyarto,
rendeles.rendeles_termek,
rendeles.rendeles_mennyiseg,
rendeles.rendeles_szam,
rendeles.rendeles_status,
rendeles.rendeles_created,
rendeles.rendeles_visszaig,
gyarto.gyarto_nev,
termek.termek_nev,
termek.termek_egyseg,
(SELECT SUM(foglalas.foglalas_mennyiseg) AS foglalt FROM foglalas
WHERE foglalas.foglalas_rendeles_id = rendeles.rendeles_id)
FROM rendeles
LEFT JOIN gyarto ON rendeles.rendeles_gyarto = gyarto.gyarto_id
LEFT JOIN termek ON rendeles.rendeles_termek = termek.termek_id
ORDER BY rendeles_id DESC LIMIT $actual, $row_per_page
";
You need your name after your subquery
....
( SELECT SUM(foglalas.foglalas_mennyiseg)
FROM foglalas
WHERE foglalas.foglalas_rendeles_id = rendeles.rendeles_id
) AS foglalt
....
SELECT * FROM bn_garant_tpl
JOIN bn_garant_val
WHERE bn_garant_tpl.what_tpl IN (1, 2)
AND bn_garant_tpl.id_tpl = bn_garant_val.id_garant_tpl
ORDER BY ords ASC
how to get lines where what_tpl = 1?
problem is next: in table bn_garant_val not exist lines if what_tpl = 1. just for if what_tpl = 2.
this query return if ID's match
AND bn_garant_tpl.id_tpl = bn_garant_val.id_garant_tpl
but, in result must be: if what_tpl = 1 also
If you need records from bn_garant_tpl even if no such record in bn_garant_val, use LEFT JOIN:
SELECT * FROM bn_garant_tpl
LEFT JOIN bn_garant_val ON bn_garant_tpl.id_tpl = bn_garant_val.id_garant_tpl
WHERE bn_garant_tpl.what_tpl IN (1, 2)
ORDER BY ords ASC
I have this query:
SELECT `y78f2_students`.`firstname` , `y78f2_students`.`lastName` , `y78f2_students`.`student_id`,`y78f2_attendance`.`is_present`, `y78f2_attendance`.`note`, `y78f2_attendance`.`thedate`
FROM `y78f2_students`
INNER JOIN `y78f2_enrolls` ON `y78f2_enrolls`.`student_id` = `y78f2_students`.`student_id`
INNER JOIN `y78f2_attendance` ON `y78f2_attendance`.`student_id` = `y78f2_students`.`student_id`
WHERE `y78f2_enrolls`.`term` = 'Term 2 2016'
AND `y78f2_enrolls`.`crn_class` = 'Math1R1'
and `y78f2_attendance`.`thedate` = '2016-01-24'
ORDER BY thedate desc
This query returns only the rows where the date is '2016-01-24'. Currently that is just one row: http://i.imgur.com/jRTBqQ0.png
I need to show all rows where term = 'Term 2 2016' and crn_class` = 'Math1R1' and also where the date is not set as yet. In order words I want to show all students in the class and if the date is not set for these students yet, it will show null.
This is what I would like: http://i.imgur.com/jmsuSF5.png
So in summary I need to show rows where the clause is met and those where the date would be null or not exist yet.
How can I write this query?
Try moving the conditions related to the joined tables from the end of the query, up to the table's respective ON clause for each join. Also, if you would like to return records for which no row yet exists in the y78f2_attendance table, that table should be LEFT OUTER joined, not INNER joined.
SELECT `y78f2_students`.`firstname` , `y78f2_students`.`lastName`,
`y78f2_students`.`student_id`,`y78f2_attendance`.`is_present`,
`y78f2_attendance`.`note`, `y78f2_attendance`.`thedate`
FROM `y78f2_students`
INNER JOIN `y78f2_enrolls` ON
`y78f2_enrolls`.`student_id` = `y78f2_students`.`student_id`
AND `y78f2_enrolls`.`crn_class` = 'Math1R1'
LEFT OUTER JOIN `y78f2_attendance` ON
`y78f2_attendance`.`student_id` = `y78f2_students`.`student_id`
AND (`y78f2_attendance`.`thedate` IS NULL OR `y78f2_attendance`.`thedate` = '2016-01-24')
WHERE `y78f2_enrolls`.`term` = 'Term 2 2016'
ORDER BY thedate desc
I am getting data from the server. I want that user may get only the last 5 records in the tables.
Here is the query I am currently using; it is not working:
$query = mysql_query("
SELECT
ml.PostID,
ml.UserID,
ml.PostDate,
ml.PostTime,
ml.PostCategory,
ml.PostSubCategory,
ml.PostComments,
cat.UserName
FROM UserPosts AS ml
LEFT JOIN UserNames cat
ON cat.UserID = ml.UserID
ORDERD BY DESC LIMIT 5");
Gives following error
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/content/i/h/u/ihus235/html/cs/pah_brd_v1/productivo/getPosts.php on line 77
add column name in order by clause
SELECT ml.PostID,
ml.UserID,
ml.PostDate,
ml.PostTime,
ml.PostCategory,
ml.PostSubCategory,
ml.PostComments,
cat.UserName
FROM UserPosts AS ml
LEFT JOIN UserNames cat
ON cat.UserID = ml.UserID
ORDER BY ml.PostComments DESC
LIMIT 5
$sql = 'SELECT ml.PostID,
ml.UserID,
ml.PostDate,
ml.PostTime,
ml.PostCategory,
ml.PostSubCategory,
ml.PostComments,
cat.UserName
FROM UserPosts AS ml
LEFT JOIN UserNames cat
ON cat.UserID = ml.UserID
ORDER BY ml.PostTime DESC LIMIT 5';
$query = mysql_query($sql) or die(mysql_error());
Try this.