dowjgrm6787 2014-10-28 21:10
浏览 36
已采纳

mysql(在一行中添加多个值)android app

I am trying to make a function in my android app that connects to mysql and do the following:

i have two tables in my DB:

CREATE TABLE IF NOT EXISTS `courses` (
`course_id` int(11) NOT NULL,
  `course_name` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

and:

CREATE TABLE IF NOT EXISTS `users` (
  `uid` int(11) NOT NULL,
  `unique_id` varchar(23) NOT NULL,
  `firstname` varchar(50) NOT NULL,
  `lastname` varchar(50) NOT NULL,
  `dept` varchar(20) NOT NULL,
  `email` varchar(100) NOT NULL,
  `encrypted_password` varchar(80) NOT NULL,
  `salt` varchar(10) NOT NULL,
  `created_at` datetime DEFAULT NULL,
  `type` varchar(20) NOT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;

The idea is the user type = instructor will add a course and the user type = student will join the course , many students will join many courses and any course will have many students , now my function is called my courses is that when the student clicks on it,it will give him a list of all courses that he joined ,so how can i do that in my php script?

sorry for my bad english :)

thanks in advance

  • 写回答

2条回答 默认 最新

  • dshnx48866 2014-10-28 21:36
    关注

    As Mohamed mentioned, you'll need a many-to-many relationship in your database. This means you'll need an intermediary table. Let's call this the users_courses table.

    At a minimum, this table will need two fields:

    1. uid - This serves as a foreign key to the users table.
    2. course_id - This serves as a foreign key to the courses table.

    Whenever a user adds a course, an record will be added with the user and course ids respectively.

    One way to access the data is through a join statement:

    SELECT <fields you need>
    FROM users
    INNER JOIN users_courses
    ON users.uid = users_courses.course_id
    INNER JOIN courses
    ON users_courses.course_id = courses.course_id
    WHERE <your conditions>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64