Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL delete all items of a person without deleting this id of the person
    primarykey
    data
    text
    <p>This is a second question , as I am trying to build on <a href="https://stackoverflow.com/questions/19138213/sql-column-with-multiple-values-query-implementation-in-a-cpp-file">this</a> one.</p> <p>So, I want to delete all items of a person without deleting this id of the person (so that he can buy new items again when he chooses to).</p> <p>I have those 3 tables : </p> <pre><code>CREATE TABLE Persons(PersonId INT not null auto_increment primary key); CREATE TABLE Items(ItemId INT not null primary key); CREATE TABLE PersonsItems(PersonsItemsId int not null auto_increment primary key, Person_Id int not null, Item_Id int not null, constraint fk_Person_id foreign key (Person_Id) references Persons(PersonId), constraint fk_Item_Id foreign key (Item_Id) references Items(ItemId)); </code></pre> <p>I then fill the tables.</p> <p>and the last table like that: </p> <pre><code>INSERT INTO PersonsItems(Person_Id, Item_Id ) VALUES (1,5),(1,8),(1,10) </code></pre> <p>So then, I have into Person with Id=1 three items, the items with ID 5, 8 and 10.</p> <p>I want with a statement to delete all items where the Id of the Person is 1. Then, the person has no items at all but in the future if he wishes to, he can take some items again.</p> <pre><code>mysql_query(connection, \ " DELETE * FROM PersonsItems(PersonsItemsId, Person_Id, Item_Id) WHERE PersonId = 1"); printf("%ld Row(s) Deleted!\n", (long) mysql_affected_rows(connection)); //See remaining rows mysql_query(connection, \ "SELECT Person_Id, Item_Id FROM PersonsItems"); //Resource struct with rows of returned data. resource = mysql_use_result(connection); // Fetch single result result = mysql_fetch_row(resource); // Display Persons's new items while((result = mysql_fetch_row(resource))) { printf("%s %s\n",result[0],result[1]); } </code></pre> <p>produces this </p> <pre><code>-1 Row(s) Deleted! 1 5 1 8 1 10 mysql&gt; SELECT * FROM PersonsItems; +----------------+-----------+----------+ | PersonsItemsId | Person_Id | Item_id | +----------------+-----------+----------+ | 201 | 1 | 5 | | 202 | 1 | 8 | | 203 | 1 | 10 | +----------------+---------+------------+ </code></pre> <p>So, it deleted none . I would like all fields to be deleted but without PersonId 1 to be deleted at all.</p> <p>Can this happen? Thanks in advance. PS: I have been working on that some days now.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload