Hi!
Like the subject says, is this possible to do this in one query?
I want to select three projects projects sorted by the 'ordering'
field and for each of this projects I want three rows from the
'content' table selcted by 'projectId' ordered by ordering.
Here is the two tables I use, the way I do it now, and the result:
--
-- Table structure for table `project`
--
CREATE TABLE `project` (
`id` int(11) NOT NULL auto_increment,
`title` varchar(255) NOT NULL,
`customer` varchar(255) NOT NULL,
`description` text NOT NULL,
`published` tinyint(1) NOT NULL default '1',
`ordering` int(11) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=3DMyISAM DEFAULT CHARSET=3Dlatin1 AUTO_INCREMENT=3D137 ;
--
-- Dumping data for table `project`
--
INSERT INTO `project` (`id`, `title`, `customer`, `description`,
`published`, `ordering`) VALUES
(135, 'FESTEMEKANISME / NETTKROK', 'SCANSIS AS', 'Fotballm=E5l,
ballbinger etc. ordreproduserer spr=F8ytest=F8pte produkter i thermoplast
med vekt fra 0,5 til 1000 gram i st=F8pemaskiner med lukketrykk fra 22
tonn til 330 tonn. Vi arbeider med alle typer oppdrag innenfor alle
typer bransjer, og vi st=F8per ogs=E5 detaljer med formverkt=F8y produsert
av andre leverand=F8rer. \r\n\r\nForm-Tek produserer st=F8peverkt=F8y i ege=
t
maskineringsverksted. Vi bruker CAD / CAM gjennom hele prosessen til
ferdig formverkt=F8y. ', 1, 3),
(131, 'BATTERIPAKKE TIL FORSVARET', 'GYLLING TEKNIKK AS', 'Forsvaret
ordreproduserer spr=F8ytest=F8pte produkter i thermoplast med vekt fra 0,5
til 1000 gram i st=F8pemaskiner med lukketrykk fra 22 tonn til 330 tonn.
Vi arbeider med alle typer oppdrag innenfor alle typer bransjer, og vi
st=F8per ogs=E5 detaljer med formverkt=F8y produsert av andre leverand=F8re=
r.
\r\n\r\nForm-Tek produserer st=F8peverkt=F8y i eget maskineringsverksted.
Vi bruker CAD / CAM gjennom hele prosessen til ferdig formverkt=F8y. ',
1, 1),
(130, 'CAT5 VEGGUTTAK', 'ELKO AS', 'Vi har laget CAT5 vegguttaksbokser
for Elko i en =E5rrekke.', 1, 4),
(136, 'NORR=D8NA SPORT AS', 'DETALJER TIL SEKK', 'Norr=F8na Sport
ordreproduserer spr=F8ytest=F8pte produkter i thermoplast med vekt fra 0,5
til 1000 gram i st=F8pemaskiner med lukketrykk fra 22 tonn til 330 tonn.
Vi arbeider med alle typer oppdrag innenfor alle typer bransjer, og vi
st=F8per ogs=E5 detaljer med formverkt=F8y produsert av andre leverand=F8re=
r.
\r\n\r\nForm-Tek produserer st=F8peverkt=F8y i eget maskineringsverksted.
Vi bruker CAD / CAM gjennom hele prosessen til ferdig formverkt=F8y. ',
1, 2);
--
-- Table structure for table `content`
--
CREATE TABLE `content` (
`contentId` int(11) NOT NULL auto_increment,
`projectId` int(11) NOT NULL,
`img` text NOT NULL,
`ordering` int(11) NOT NULL default '0',
PRIMARY KEY (`contentId`)
) ENGINE=3DMyISAM DEFAULT CHARSET=3Dutf8 COMMENT=3D'img'
AUTO_INCREMENT=3D133 ;
--
-- Dumping data for table `content`
--
INSERT INTO `content` (`contentId`, `projectId`, `img`, `ordering`)
VALUES
(127, 135, 'DSC_0011.jpg', 6),
(131, 131, 'DSC_00011.jpg', 2),
(126, 131, 'DSC_0004.jpg', 7),
(130, 136, 'DSC_0002.jpg', 4),
(129, 136, 'DSC_00111.jpg', 8),
(125, 136, 'DSC_0001.jpg', 3),
(128, 130, 'DSC_0014.jpg', 5),
(132, 135, 'DSC_0010.jpg', 1);
This is the way I do it now:
$conn =3D mysql_connect( 'server', 'user', 'password' );
mysql_select_db( 'database', $conn );
$query1 =3D "SELECT p.id, p.title, p.customer, p.ordering
FROM project p
WHERE p.published =3D 1
ORDER BY p.ordering LIMIT 3 ";
$result1 =3D mysql_query( $query1, $conn );
$projects =3D array();
while( $projectRow =3D mysql_fetch_object( $result1 ) )
{
$project =3D array();
$project['numRows'] =3D count( $projectRow );
$project['projectId'] =3D $projectRow->id;
$project['title'] =3D $projectRow->title;
$project['customer'] =3D $projectRow->customer;
$project['imgs'] =3D array();
$query2 =3D "SELECT c.projectId, c.img, c.ordering
FROM content c
ORDER BY c.ordering LIMIT 3 ";
$result2 =3D mysql_query( $query2, $conn );
while( $contentRow =3D mysql_fetch_object( $result2 ) )
{
$imgArray =3D array();
$imgArray['img'] =3D $contentRow->img;
$imgArray['ordering'] =3D $contentRow->contentordering;
$imgArray['projectId'] =3D $contentRow->id;
array_push( $project['imgs'], $imgArray);
}
array_push( $projects, $project );
}
foreach( $projects as $pro )
{
echo 'projectId =3D ' . $pro['projectId'] . '
';
foreach( $pro['imgs'] as $img )
{
echo 'imagename =3D ' . $img['img'] . '
';
}
echo '=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
';
}
This outputs:
projectId =3D 131
imagename =3D DSC_0010.jpg
imagename =3D DSC_00011.jpg
imagename =3D DSC_0001.jpg
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
projectId =3D 136
imagename =3D DSC_0010.jpg
imagename =3D DSC_00011.jpg
imagename =3D DSC_0001.jpg
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
projectId =3D 135
imagename =3D DSC_0010.jpg
imagename =3D DSC_00011.jpg
imagename =3D DSC_0001.jpg
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
On 27 Okt, 14:55, Thomas Gabrielsen wrote:
> Hi!
>
> Like the subject says, is this possible to do this in one query?
>
> I want to select three projects projects sorted by the 'ordering'
> field and for each of this projects I want three rows from the
> 'content' table selcted by 'projectId' ordered by ordering.
>
> Here is the two tables I use, the way I do it now, and the result:
>
> --
> -- Table structure for table `project`
> --
>
> CREATE TABLE `project` (
> =A0 `id` int(11) NOT NULL auto_increment,
> =A0 `title` varchar(255) NOT NULL,
> =A0 `customer` varchar(255) NOT NULL,
> =A0 `description` text NOT NULL,
> =A0 `published` tinyint(1) NOT NULL default '1',
> =A0 `ordering` int(11) NOT NULL default '0',
> =A0 PRIMARY KEY =A0(`id`)
> ) ENGINE=3DMyISAM =A0DEFAULT CHARSET=3Dlatin1 AUTO_INCREMENT=3D137 ;
>
> --
> -- Dumping data for table `project`
> --
>
> INSERT INTO `project` (`id`, `title`, `customer`, `description`,
> `published`, `ordering`) VALUES
> (135, 'FESTEMEKANISME / NETTKROK', 'SCANSIS AS', 'Fotballm=E5l,
> ballbinger etc. ordreproduserer spr=F8ytest=F8pte produkter i thermoplast
> med vekt fra 0,5 til 1000 gram i st=F8pemaskiner med lukketrykk fra 22
> tonn til 330 tonn. Vi arbeider med alle typer oppdrag innenfor alle
> typer bransjer, og vi st=F8per ogs=E5 detaljer med formverkt=F8y produser=
t
> av andre leverand=F8rer. \r\n\r\nForm-Tek produserer st=F8peverkt=F8y i e=
get
> maskineringsverksted. =A0Vi bruker CAD / CAM gjennom hele prosessen til
> ferdig formverkt=F8y. =A0', 1, 3),
> (131, 'BATTERIPAKKE TIL FORSVARET', 'GYLLING TEKNIKK AS', 'Forsvaret
> ordreproduserer spr=F8ytest=F8pte produkter i thermoplast med vekt fra 0,=
5
> til 1000 gram i st=F8pemaskiner med lukketrykk fra 22 tonn til 330 tonn.
> Vi arbeider med alle typer oppdrag innenfor alle typer bransjer, og vi
> st=F8per ogs=E5 detaljer med formverkt=F8y produsert av andre leverand=F8=
rer.
> \r\n\r\nForm-Tek produserer st=F8peverkt=F8y i eget maskineringsverksted.
> Vi bruker CAD / CAM gjennom hele prosessen til ferdig formverkt=F8y. ',
> 1, 1),
> (130, 'CAT5 VEGGUTTAK', 'ELKO AS', 'Vi har laget CAT5 vegguttaksbokser
> for Elko i en =E5rrekke.', 1, 4),
> (136, 'NORR=D8NA SPORT AS', 'DETALJER TIL SEKK', 'Norr=F8na Sport
> ordreproduserer spr=F8ytest=F8pte produkter i thermoplast med vekt fra 0,=
5
> til 1000 gram i st=F8pemaskiner med lukketrykk fra 22 tonn til 330 tonn.
> Vi arbeider med alle typer oppdrag innenfor alle typer bransjer, og vi
> st=F8per ogs=E5 detaljer med formverkt=F8y produsert av andre leverand=F8=
rer.
> \r\n\r\nForm-Tek produserer st=F8peverkt=F8y i eget maskineringsverksted.
> Vi bruker CAD / CAM gjennom hele prosessen til ferdig formverkt=F8y. ',
> 1, 2);
>
> --
> -- Table structure for table `content`
> --
>
> CREATE TABLE `content` (
> =A0 `contentId` int(11) NOT NULL auto_increment,
> =A0 `projectId` int(11) NOT NULL,
> =A0 `img` text NOT NULL,
> =A0 `ordering` int(11) NOT NULL default '0',
> =A0 PRIMARY KEY =A0(`contentId`)
> ) ENGINE=3DMyISAM =A0DEFAULT CHARSET=3Dutf8 COMMENT=3D'img'
> AUTO_INCREMENT=3D133 ;
>
> --
> -- Dumping data for table `content`
> --
>
> INSERT INTO `content` (`contentId`, `projectId`, `img`, `ordering`)
> VALUES
> (127, 135, 'DSC_0011.jpg', 6),
> (131, 131, 'DSC_00011.jpg', 2),
> (126, 131, 'DSC_0004.jpg', 7),
> (130, 136, 'DSC_0002.jpg', 4),
> (129, 136, 'DSC_00111.jpg', 8),
> (125, 136, 'DSC_0001.jpg', 3),
> (128, 130, 'DSC_0014.jpg', 5),
> (132, 135, 'DSC_0010.jpg', 1);
>
> This is the way I do it now:
>
> $conn =3D mysql_connect( 'server', 'user', 'password' );
> mysql_select_db( 'database', $conn );
>
> $query1 =3D =A0 =A0 =A0 "SELECT p.id, p.title, p.customer, p.ordering
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 FROM project p
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 WHERE p.published =3D 1
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ORDER BY p.ordering LIMIT=
3 ";
>
> $result1 =3D mysql_query( $query1, $conn );
> $projects =3D array();
> while( $projectRow =3D mysql_fetch_object( $result1 ) )
> {
> =A0 =A0 =A0 =A0 $project =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0=3D array();
> =A0 =A0 =A0 =A0 $project['numRows'] =A0 =A0 =A0 =A0 =A0 =A0 =3D count( $p=
rojectRow );
> =A0 =A0 =A0 =A0 $project['projectId'] =A0 =3D $projectRow->id;
> =A0 =A0 =A0 =A0 $project['title'] =A0 =A0 =A0 =A0 =A0 =A0 =A0 =3D $projec=
tRow->title;
> =A0 =A0 =A0 =A0 $project['customer'] =A0 =A0=3D $projectRow->customer;
> =A0 =A0 =A0 =A0 $project['imgs'] =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=3D array=
();
>
> =A0 =A0 =A0 =A0 $query2 =3D =A0 =A0 =A0 "SELECT c.projectId, c.img, c.ord=
ering
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 FROM cont=
ent c
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ORDER BY =
c.ordering LIMIT 3 ";
>
> =A0 =A0 =A0 =A0 $result2 =3D mysql_query( $query2, $conn );
> =A0 =A0 =A0 =A0 while( $contentRow =3D mysql_fetch_object( $result2 ) )
> =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 $imgArray =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =3D array();
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 $imgArray['img'] =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0=3D $contentRow->img;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 $imgArray['ordering'] =A0 =3D $contentRow=
->contentordering;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 $imgArray['projectId'] =A0=3D $contentRow=
->id;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 array_push( $project['imgs'], $imgArray);
> =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 array_push( $projects, $project );
>
> }
>
> foreach( $projects as $pro )
> {
> =A0 =A0 =A0 =A0 echo 'projectId =3D ' . $pro['projectId'] . '
';
> =A0 =A0 =A0 =A0 foreach( $pro['imgs'] as $img )
> =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 echo 'imagename =3D ' . $img['img'] . ' r />';
> =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 echo '=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
';
>
> }
>
> This outputs:
>
> projectId =3D 131
> imagename =3D DSC_0010.jpg
> imagename =3D DSC_00011.jpg
> imagename =3D DSC_0001.jpg
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> projectId =3D 136
> imagename =3D DSC_0010.jpg
> imagename =3D DSC_00011.jpg
> imagename =3D DSC_0001.jpg
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> projectId =3D 135
> imagename =3D DSC_0010.jpg
> imagename =3D DSC_00011.jpg
> imagename =3D DSC_0001.jpg
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
There was an error in the first message. query2 should have a WHERE
clause like this:
$query2 =3D "SELECT c.projectId, c.img, c.ordering
FROM content c
WHERE c.projectId =3D $projectRow->id
ORDER BY c.ordering LIMIT 3 ";
Thanks,
Thomas