• Linux mysql 联表查询


    在rhce考试题中,第21、22题为数据库查询题

    题目:

     在system1上创建一个Maria DB数据库,名为Contacts,要求:

    • 数据库应该包含来自数据库users.mdb的内容,数据库只能被localhost访问
    • 除了root用户,此数据库只能被用户Marry查询,此用户密码为redhat
    • root用户的数据库密码为redhat,同时不允许空密码登录
    • 密码是fadora的人的名字是什么?
    • 有多少人姓名是John,同时居住在Santa Clara?

    为了完成这个实验,我已将user.mdb放到本文最下方,复制保存为user.mdb,在数据库中source 该文件即可执行

    该文章主要看第四和第五个问题

     

    1、密码是fadora的人的名字

    SELECT
        u_name.firstname 
    FROM
        u_name,
        u_passwd 
    WHERE
        u_name.userid = u_passwd.uid 
        AND u_passwd.PASSWORD = 'fadora';

      

    2、有多少名为John并且居住在Santa Clara的

    SELECT
        COUNT(*) 
    FROM
        u_name,
        u_loc 
    WHERE
        u_name.userid = u_loc.uid 
        AND u_name.firstname = 'John' 
        AND u_loc.location = 'Santa Clara'

    COUNT(*) 统计排除值为null的行的个数
    COUNT(1) 统计第一个字段不会null的行的个数

    讲解:

        SELECT          执行顺序为最后 ,显示以下条件符合之后需要的值
            u_name.firstname    
        FROM            执行顺序为第一,在from下的表中查询
            u_name,             
            u_passwd 
        WHERE           执行顺序为第二 ,查询from下的表符合条件的
            u_name.userid = u_passwd.uid 
            AND u_passwd.PASSWORD = 'fadora';

    参考2得出的结论

    SELECT
        你想要的东西 
    FROM
        这些东西有关的所有的表 
    WHERE
        这些表中的相同条件 
        AND 某张表中单独的条件 
        AND 某张表中单独的条件

    连表查询:
    找到将多张表关联起来的字段,然后查询条件中写使关联的字段相等,最后写条件

     

    练习3

    查询密码是fedora,并且住址是Santa Clara的人

    SELECT
        res.firstname 
    FROM
        ( SELECT * FROM u_name, u_passwd WHERE u_name.userid = u_passwd.uid AND u_passwd.PASSWORD = 'fadora' ) res,
        u_loc 
    WHERE
        res.userid = u_loc.uid 
        AND u_loc.LOCATION = 'Santa Clara';

    解析:

      res为新表,res.firstname 为新表中的字段

      首先查询所有密码是fadora的人,然后将查询结果作为新表,命名为res,然后继续联表查询

    练习例子:

    有a,b,c三张表
    a(uid,name)
    b(uid.password)
    c(name.local)

     

    三张表关联为a和b通过uid关联,a和c通过name关联,b和c无关联

    查询namejerry,passwordsun并且localfour road的人名字

    SELECT
         res.a_name
    FROM
        ( SELECT name.a_name FROM name, passwd WHERE name.a_uid = passwd.u_uid AND passwd.u_passwd = 'sun' ) res ,local
        where
        res.a_name = local.a_name 
        AND local.a_local = 'four road'

      

    res为新表别名

    或者可以写为

    SELECT
         res.a_name
    FROM
        ( SELECT name.a_name FROM name, passwd WHERE name.a_uid = passwd.u_uid AND passwd.u_passwd = 'sun' ) res left join local ON 
        res.a_name = local.a_name 
        AND local.a_local = 'four road'

    user.mdb文件内容

    -- MySQL dump 10.14  Distrib 5.5.35-MariaDB, for Linux (x86_64)
    --
    -- Host: localhost    Database: Contacts
    -- ------------------------------------------------------
    -- Server version    5.5.35-MariaDB
    
    /*!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 utf8 */;
    /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
    /*!40103 SET TIME_ZONE='+00:00' */;
    /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
    /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
    /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
    /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
    
    --
    -- Table structure for table `u_name`
    --
    
    DROP TABLE IF EXISTS `u_name`;
    /*!40101 SET @saved_cs_client     = @@character_set_client */;
    /*!40101 SET character_set_client = utf8 */;
    CREATE TABLE `u_name` (
      `userid` int(11) NOT NULL AUTO_INCREMENT,
      `firstname` varchar(50) NOT NULL,
      `lastname` varchar(50) NOT NULL,
      PRIMARY KEY (`userid`)
    ) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=latin1;
    /*!40101 SET character_set_client = @saved_cs_client */;
    
    --
    -- Dumping data for table `u_name`
    --
    
    LOCK TABLES `u_name` WRITE;
    /*!40000 ALTER TABLE `u_name` DISABLE KEYS */;
    INSERT INTO `u_name` VALUES (1,'san','zhang'),(2,'si','li'),(3,'wu','wang'),(4,'Barack','Obama'),(5,'George','Walker Bush'),(6,'Bill','Clinton'),(7,'Hillary','Clinton'),(8,'John','Clinton'),(9,'George','wang'),(10,'John','li'),(11,'Bill','wang'),(12,'George','Obama'),(13,'George','wang'),(14,'Michael','Jackson'),(15,'John','Clinton'),(16,'Michael','Walker Bush'),(17,'Michael','Obama'),(18,'Hillary','Jackson'),(19,'Georgexx','Jackson'),(20,'Barack','Jackson'),(21,'John','Jackson'),(22,'John','Obama'),(23,'John','Clinton'),(24,'John','Walker Bush'),(25,'John','wang');
    /*!40000 ALTER TABLE `u_name` ENABLE KEYS */;
    UNLOCK TABLES;
    
    --
    -- Table structure for table `u_passwd`
    --
    
    DROP TABLE IF EXISTS `u_passwd`;
    /*!40101 SET @saved_cs_client     = @@character_set_client */;
    /*!40101 SET character_set_client = utf8 */;
    CREATE TABLE `u_passwd` (
      `uid` int(11) NOT NULL AUTO_INCREMENT,
      `password` varchar(50) NOT NULL,
      PRIMARY KEY (`uid`)
    ) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=latin1;
    /*!40101 SET character_set_client = @saved_cs_client */;
    
    --
    -- Dumping data for table `u_passwd`
    --
    
    LOCK TABLES `u_passwd` WRITE;
    /*!40000 ALTER TABLE `u_passwd` DISABLE KEYS */;
    INSERT INTO `u_passwd` VALUES (1,'redhat'),(2,'fedora'),(3,'centos'),(4,'centes'),(5,'redhrt'),(6,'ridhat'),(7,'redfat'),(8,'fadora'),(9,'cantos'),(10,'redhap'),(11,'contos'),(12,'fcdora'),(13,'cendora'),(14,'tangene'),(15,'tangrine'),(16,'tangerone'),(17,'tangeine'),(18,'taangerine'),(19,'tangerine'),(20,'tanggerine'),(21,'anggerine'),(22,'aggerine'),(23,'taggerine'),(24,'tanerine'),(25,'tannerine');
    /*!40000 ALTER TABLE `u_passwd` ENABLE KEYS */;
    UNLOCK TABLES;
    
    --
    -- Table structure for table `u_loc`
    --
    
    DROP TABLE IF EXISTS `u_loc`;
    /*!40101 SET @saved_cs_client     = @@character_set_client */;
    /*!40101 SET character_set_client = utf8 */;
    CREATE TABLE `u_loc` (
      `uid` int(11) NOT NULL AUTO_INCREMENT,
      `location` varchar(50) NOT NULL,
      PRIMARY KEY (`uid`)
    ) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=latin1;
    /*!40101 SET character_set_client = @saved_cs_client */;
    
    --
    -- Dumping data for table `u_loc`
    --
    
    LOCK TABLES `u_loc` WRITE;
    /*!40000 ALTER TABLE `u_loc` DISABLE KEYS */;
    INSERT INTO `u_loc` VALUES (1,'Shenzhen'),(2,'Guangzhou'),(3,'Santa Clara'),(4,'Santa Clara'),(5,'San Francisco'),(6,'Santa Calara'),(7,'Santa Clare'),(8,'Santa Clara'),(9,'Florida'),(10,'Santa Claraa'),(11,'Florida'),(12,'State of Texas'),(13,'State of Texas'),(14,'Santa Claraa'),(15,'Santa Clara'),(16,'Colorado?CO'),(17,'Hawaii?HI'),(18,'Santa Calara'),(19,'Santa Clra'),(20,'Santa Clara'),(21,'Santa Clara'),(22,'Minnesota'),(23,'Vermont'),(24,'Santa Clara'),(25,'Minnesota');
    /*!40000 ALTER TABLE `u_loc` ENABLE KEYS */;
    UNLOCK TABLES;
    /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
    
    /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
    /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
    /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
    /*!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 */;
    /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
    
    -- Dump completed on 2016-07-24 18:17:53
  • 相关阅读:
    java spring-WebSocket json参数传递与接收
    java实现zabbix接口开发
    Linux 系统中的MySQL数据库默认区分大小写
    获取Linux下的IP地址 java代码
    Java多线程问题总结
    Easyui之accordion修改Title样式,字体等
    机器学习算法随机数据生成
    神经网络MPLClassifier分类
    给定数据利用神经网络算法模型进行计算
    vue $refs获取dom元素
  • 原文地址:https://www.cnblogs.com/despotic/p/10689450.html
Copyright © 2020-2023  润新知