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 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看