j'ai une Table `students` et une table `logaccess`.
Je veux sélectionner tous mes `students`.`idbadge` selon une condition particulière.
Explication : dans ma table `logaccess` j'ai des `idbadge`type INT et un `horodate` type TIMESTAMP.
Je veux afficher dans une view (je suis en mySQL 5.0)
col1 `idbadge` et col2 `horodate`
Dans ma col1 je veux tous les students.idbadge avec dans col2 leurs logaccess.horodate correspondants.
Mais pour les étudiants qui n'ont pas de horodate....ben je veux afficher un NULL est ce possible ?
<!--c1-->
<!--ec1-->DROP VIEW IF EXISTS `badge_log`.`vw_test`;
CREATE
ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER
VIEW `vw_test` AS select sql_no_cache `students`.`idbadge` AS
`idbadge`,cast(`logaccess`.`horodate` as date) AS `date` from
`students`,`logaccess` WHERE `logaccess`.`idbadge`=`students`.`idbadge`;<!--c2-->
<!--ec2-->
Dans mon WHERE je sais pas quoi utilisé car ici je ne selectionne que ceux qui ont un horodate dans logaccess.
A voir également:
Affichage dans une view avec une condition particulière
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY
DEFINER VIEW `vw_test` AS select sql_no_cache `students`.`idbadge` AS
`idbadge`,cast(`logaccess`.`horodate` as date) AS `date` from
`students`,`logaccess` WHERE `logaccess`.`idbadge`=`students`.`idbadge`
UNION
select `students`.`idbadge` AS `idbadge`, NULL AS `date` from
`students` WHERE `idbadge` NOT IN (SELECT `idbadge` from `logaccess`)
Ca s'appelle une jointure externe :
remplace "from `students`,`logaccess` WHERE `logaccess`.`idbadge`=`students`.`idbadge`;"
par " FROM Students LEFT OUTER JOIN logaccess ON logacess.idbadge = students.idbadge"
<!--c2--><!--ec2-->