Ce l'ho fatta !!! Ma non prende le notizie senza commenti.
codice:
select * from (select @c:=0, @d:=-1, @a:=0 ) as _ROWS
inner join (select @c:=@c+1 AS num_row, id_master, title, @e:=-1 from `master`) A on 1=1
inner join ( select * from (
SELECT D1.id_master, min(num_com) AS minc, max(num_com) AS maxc from
(select DISTINCT `id_master`, @f:=0 from `detail`) as D1
inner join
(select @f:=@f+1 AS num_com, id_master from `detail`) as D2 on D1.id_master = D2.id_master
group by D1.id_master
) AS D3
) as D0 on A.id_master = D0.id_master
inner join (select @a:=@a+1 AS num_com2, id_master, `comment` from `detail`) C on A.id_master = C.id_master
where num_row <= 4 and num_com2 < (minc + 3)
order by num_com2
tabella master :
codice:
-- phpMyAdmin SQL Dump
-- version 4.7.0
-- https://www.phpmyadmin.net/
--
-- Hôte : 127.0.0.1
-- Généré le : ven. 02 juin 2017 à 12:54
-- Version du serveur : 5.7.17
-- Version de PHP : 7.1.3
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Base de données : `tests`
--
-- --------------------------------------------------------
--
-- Structure de la table `master`
--
CREATE TABLE `master` (
`id_master` int(11) NOT NULL,
`title` varchar(50) COLLATE latin1_bin NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_bin;
--
-- Déchargement des données de la table `master`
--
INSERT INTO `master` (`id_master`, `title`) VALUES
(1, 'argomento 1'),
(2, 'argomento 2'),
(3, 'argomento 3'),
(4, 'argomento 4'),
(5, 'argomento 5'),
(6, 'argomento 6'),
(7, 'argomento 7');
--
-- Index pour les tables déchargées
--
--
-- Index pour la table `master`
--
ALTER TABLE `master`
ADD PRIMARY KEY (`id_master`);
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
tabella detail
codice:
-- phpMyAdmin SQL Dump
-- version 4.7.0
-- https://www.phpmyadmin.net/
--
-- Hôte : 127.0.0.1
-- Généré le : ven. 02 juin 2017 à 12:53
-- Version du serveur : 5.7.17
-- Version de PHP : 7.1.3
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Base de données : `tests`
--
-- --------------------------------------------------------
--
-- Structure de la table `detail`
--
CREATE TABLE `detail` (
`id_detail` int(11) NOT NULL,
`id_master` int(11) NOT NULL,
`comment` varchar(50) COLLATE latin1_bin NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_bin;
--
-- Déchargement des données de la table `detail`
--
INSERT INTO `detail` (`id_detail`, `id_master`, `comment`) VALUES
(1, 1, 'commento 1 argomento 1'),
(2, 1, 'commento 2 argomento 1'),
(5, 2, 'commento 1 argomento 2'),
(6, 2, 'commento 2 argomento 2'),
(7, 2, 'commento 3 argomento 2'),
(8, 2, 'commento 4 argomento 2'),
(9, 3, 'commento 1 argomento 3'),
(10, 3, 'commento 2 argomento 3'),
(11, 3, 'commento 3 argomento 3'),
(12, 3, 'commento 4 argomento 3'),
(13, 4, 'commento 1 argomento 4'),
(14, 4, 'commento 2 argomento 4'),
(15, 4, 'commento 3 argomento 4'),
(16, 4, 'commento 4 argomento 4'),
(17, 4, 'commento 5 argomento 4'),
(18, 5, 'commento 1 argomento 5'),
(19, 5, 'commento 2 argomento 5'),
(20, 5, 'commento 3 argomento 5'),
(21, 5, 'commento 4 argomento 5'),
(22, 5, 'commento 5 argomento 5'),
(23, 6, 'commento 1 argomento 6'),
(24, 6, 'commento 2 argomento 6');
--
-- Index pour les tables déchargées
--
--
-- Index pour la table `detail`
--
ALTER TABLE `detail`
ADD PRIMARY KEY (`id_detail`),
ADD KEY `detail01` (`id_master`);
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
Con un limite di 4 notizie con fino a 3 commenti
20170602_001.JPG
Senza limiti
20170602_002b.JPG