简体   繁体   中英

mysql (add more than one value in one row) 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

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>

Many To many relationship

And use inner join in your Sql query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM