Sana Joseph
Sana Joseph

Reputation: 1948

How to insert data to table based on a specific condition

I have 3 tables: TimeTable, Subject, WeekDay.

The TimeTable table has the following fields: SubjectId, WeekDayId.

I want to insert values into the TimeTable table, but the data comes in names, not id.s, so I want to execute this statement:

Insert into TimeTable SubjectId , WeekDayId 
Select WeekDay.WeekdayId , Subject.SubjectId from weekday , Subject 
Where Subject.SubjectName=IPSubjectName And Weekday.WeekDayName=IPWeekDayName

Now I have 2 questions:

  1. Is this statement available in sqlite (Am I writing it right)?

  2. How can I dynamically take input at these values (IPSubjectName, IPWeekDayName).
    i.e If I have var SubjectName, var WeekDayName, how will the statement be written then?

Upvotes: 1

Views: 287

Answers (2)

okay_google
okay_google

Reputation: 349

  1. Yes, I think the statement you wrote is available in SQLite.
  2. I'm assuming you wrote a PHP function which calls this query, in which case, your variable names would become $SubjectName and $WeekDayName. Then, your code will become:

    Where Subject.SubjectName=$SubjectName AND Weekday.WeekDayName=$WeekDayName
    

Upvotes: 2

praveen2609
praveen2609

Reputation: 233

Pass the values as parameters in the function where you are executing the query and replay the right hand side clause with ${paramname1} and ${paramname2}.

Upvotes: 0

Related Questions