I have 2 tables that I am working with that use the same column; one table contains the text and the other table contains the images; they use the column listing_id so that the right text shows up with the right images;
my problem is that because column listing_id is auto-increment, my first table is able to have an insert into query that is able to insert the text and then +1 the column listing_id; however the 2nd table I use another INSERT INTO query will not have the right listing_id,
because some entries for listing_id have been deleted, meaning that the 2nd table's listing_id will always be behind the 1st tables listing_id;
how do I reference the column listing_id?
You need to create an INT column called something like "parent_id" in the dependant tables that stores the id of the main table that it is referencing. When you select records from the first, you would then JOIN the tables with the auto_increment field of the first field against the "parent_id" of the second.
As MrSlayer mentions, use the newly inserted ID of the first table to update "parent_id". You should typically have a unique ID field in the second table for uniqueness, but it shouldn't be part of the relationship to the first table.
If you're unclear about how to get the id that the first table auto_increments to when you insert, use mysql_insert_id().
mysql_query("INSERT INTO table1 ...");
echo "Last inserted record_id in table1 was " . mysql_insert_id();
INSERT INTO table1 (mytextcolumn) VALUES('text');
INSERT INTO table2 (parent_id,image_name) VALUES(LAST_INSERT_ID(),'someimage.png');
Related
leave_request tablein my table emp_id column contains duplicate data 3 times. i want to select all the data from the table and the duplicate data to count as once. here i am providing the screenshot of my table structure
You can try below -
select max(id),emp_id, emp_name
from tablename
group by emp_id, emp_name
I have two tables say, Table1 and Table2.
Table1 contain 20 fileds with primary key ID for Table1 auto increment and Table2 contain same 20 column with primary key ID1 for TAble2 auto increment.
Table1- ID,Column1,Column2,Column3....,Colunm20.
Table2-ID1,ID,Column1,Column2,.....,Column20
I insert each record into Table1. I want to check duplicate Entry for Table1 if all field are same.
Table2 contain the Entry form Table1.If I modified record First record goes to Table2 as it is then it will update into Table1.(i.e first copy the record into Table2 for backup and then update into Table1) So, same condition for Duplicate entry check near Table2 also need. Please Help!
I have coded this in php.
You can write trigger for table 1 which is copying data from table2 on every insert event.
And for unique data either you can check it on php side before you insert or you can define column as unique. Your question is bit confusing so Its difficult to give exact answer.
What are the unique columns ??
I've a table with the purpose of linking two other tables with foreign keys (table 3).
table1 = id, name
table2 = id, sport
table3 = fk_table1_id, dk_table2_id
I do server side code with php and i need a way to automatic insert a row into table 3 when a row is inserted into table2.
But i dont know how to do a php query that will get the generated id of the new row in table2?(The id of table one i do have stored in a php variable)Im a looking at a smart stored procedure?
Steps to follow:
Insert a row into table2.
Find the generated key from table2 and assign it to a PHP variable.
-- read this into a php variable, say, $last_key
SELECT LAST_INSERT_ID();
Use the $last_key in insert statement for table3.
You can Follow below Steps:
1) Make ID to AUTO_INCREMENT IN Table2
2) Create Store Procedure that work as below:
A) INSERT Value to your Table2 :
B) Increase ID value : SELECT LAST_INSERT_ID() INTO VARIABLE;
C) Insert Value to your Table3 : use VARIABLE to get ID of Table2
I have one column called 'speeding' in a table containing many other columns. This column contains an integer that is foreign key to an entry on a table named Speeding. This table has columns 1-25 and an id that is referenced by the 'speeding' column from the first table.
Besides using join, is there any setting I can set on 'speeding' to make it automatically pull the associated data from the table Speeding?
You could create a view, a view is basically a SQL statement that is stored on the MySQL server and acts like a table
CREATE VIEW ViewName AS
SELECT tbl1.data, tbl2.speeding
FROM tbl1
INNER JOIN tbl2 ON tbl2.key = tbl1.key;
http://dev.mysql.com/doc/refman/5.0/en/create-view.html
You then use the view as you would use any table
SELECT data, speeding
FROM ViewName
I have a site that is essentially a collection of links. A mysql database which stores users, links and votes. My links table has two foreign keys, user_id and vote_id. When a link is inserted into the database it will start with one vote so that means I need to insert a row into the votes table but they essentially depend on one another to exist. I need the ID of the links for the foreign key of the votes table and vice versa. My current "plan" is to insert a links row with a vote_id of 0, select the same row to get it's ID and then insert a votes row using the links row id as it's foreign key, select it's ID and update my original links row. This seems really inefficient but I need to make sure users votes are recorded for time keeping and eliminating duplicate votes. Am I going about this the wrong way?
In PHP, you can use mysql_insert_id:
mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d\n", mysql_insert_id());
You could use this to get the row ID of the newly inserted link.
Assuming one vote can only belong to one link, the links table should not have a vote_id column. So you shouldn't need the identity of the newly inserted vote.