Im trying to create a table which only will have the serial and 9 int foreign keys.
Is there a way to insert the last id from other tables to this table?
i been trying with RETURNING, nextval()-1, currval(),ordering results by timestamp but nothing seems to work, i just cant get the ids values from the other tables inserted into the table.
even tried this simple set of queries for each table to update one by one of the table columns
$lst="SELECT id from table1 ORDER BY timestamp DESC limit 1";
$lst_res=pg_query($conn,$lst);
$sql_eq_bat_up="UPDATE table9 set table1_id='$lst_res[id]'";
$do_up_tbl1=pg_query($conn,$sql_eq_bat_up);
i been doing some research but had no luck, im using postgreSQL 9.5 and php for this, i hope you can help me thanks.
Related
I have one Sql Query to get all the informations from my table.
I created an list using an foreach.
And i want to order this list, by the last updated row.
Like this
$query - "SELECT * FROM table ORDER BY last_updated_row";
//call Query here
And when i updated a certain row, i want to put this row on the top of the list
I heard about time_stamp, can i use time_stamp for that?
how can i do that?
Thanks
Assuming your using MySQL your table needs to be like this
CREATE TABLE table (
last_updated_row TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
That will give the row a create time stamp and update it on each update statement which effects the row
http://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html
You can use just about any date/datetime/timestamp column in a table to sort by if needed. The only catch is you need to actually have it in the table.
Any of the above will allow sorts by ascending/descending order, but need to be maintained when inserting/updating a row.
Assuming you have the following structure:
table - someTable
id someVale updateTime
1 54634 ......
2 65138 ......
3 94141 ......
4 84351 ......
It doesn't matter what type of column updateTime is - whether it is a date, a datetime, a timestamp, a simple order by updateTime will work.
But you need to make sure that each insert/update you make to that row updates the column so that the sort will be true.
I don't have any coding related problem. I've a small doubt here related to mysql database. Below i've attached an image. I've inserted some records into database. For example totally 40 records i've inserted into mysql database. Some records deleted by the users. Problem is if i insert a new record (auto id is 41) its insert a record into middle of the another two rows. You can see my image below three row is there (7, 41 and 40). Why 41st record is inserted between 7 & 40 ? Why 41st record not inserted after 40th id?
SQL doesn't guarantee and order unless you specifically ask for one with an ORDER BY clause. If you want your rows in VoucherID order specify ORDER BY VoucherID in your SELECT:
SELECT * from MyTable ORDER BY VoucherID
it seems its related to sorting at your phpmyadmin view. just click on column header of "VoucherID" and check than. currently it seems sorted with "VoucherReference" (descending)
I have 2 tables that have one-to-one relationship in mySQL. they both have the same user_id as Primary key, and I need somehow when I insert a post into the my first table, automatically be able to insert the row with same user_id to my second table. Is there any mysql command or PHP script that I can use it ?
You might set up a TRIGGER in the database. These trigger entities are stored in the database's structure, with PHP you might only execute the CREATE TRIGGER query which creates one.
However, two tables having the exact same data as their PRIMARY KEY sounds like your database structure is a bit badly modelled. You should take time to remodel the database, essentially merging (if possible) the two tables together.
And if you are using PHP to INSERT INTO the database, you can call the two queries after each other:
INSERT INTO table1(field1, field2...) VALUES (value1, value2...)
INSERT INTO table2(field1, field2...) VALUES (value1, value2...)
But reliance on two queries after each other requires pinpoint accuracy as the primary keys might go out of sync, breaking the relations.
If I understand your question right you can get the inserted user_id from the first table and use that to insert a new row in the second table.
$query = mysql_query("SELECT * FROM first_table ORDER BY user_id DESC LIMIT 1");
$row = mysql_fetch_assoc($query);
Now $row['user_id'] can be inserted in the second table.
This could of course be risky if many rows are inserted at the same time.
I have a small PHP Mysql function which generates all the columns within a mysql table, but I would like the function not to display the primary keys for each table just the other columns.
How can this be done, I havent been able to find the code for it.
Thanks
It seems I didnt explain the question well.
The mysql table from which the columns are generated is sent on demand from a list of ALL THE TABLE IN THE DB (over 150) and I cant specify the exact columns for each of the table.
It would just be more efficient if I found a way of omitting the primary key from the result.
Since it isnt required for the subsequent processing and quite confusing to the enduser as to its use.
Thanks
Use:
SELECT column1, column2, column3 FROM table
For what it's worth, returning the PK or not isn't going to break the bank.
In general, doing SELECT * FROM is bad, but if you're just going to do SELECT every, column, but, the, pk FROM then you may as well just select everything.
The best answer is just to SELECT the columns you need. If you need 3 columns, query for 3 columns and name them explicitly: SELECT column1, column2, column3 FROM table_name
I have a table bundled among 100 databases in MYSQL (i.e. 1st x rows of the table in database_1, 2nd x rows of the table in database_2, ... , last x rows of the table in database_100)
Each table has a row whenever a user visits a friend for a game.
The columns are iuin, logtime, beuin.
iuin is the user id of the visitor.
beuin is the user id of the friend who was visited.
logtime is when the visit was made.
I would like to find the # of distinct friends who were visited during a week.
There is roughly 300k distinct users who are visited per day.
However, when I extended my code to calculate for a week, I ran out of memory.
My code basically does an SQL query using SELECT DISTINCT beuin for a selected week for the table in each database. I store all the beuin in an array if it's not already stored (so I count distinct friends who were visited), and return the size of the array at the end.
FYI, I can't edit the database around such as joining all the tables in different databases into one table.
Is there any alternative ways i can do this?
Thanks
It's hard to say anything about your without the one. But I think you can solve this problem using mysql. My quick solution:
Create table - CREATE table if not exist users_ids(user_id INT NOT NULL DEAULT 0 PRIMARY KEY(UNIQUE)); in the first db
Truncate users_ids
Run 100 queries like INSERT IGNORE INTO db1.users_ids select distinct user_id from db1.table1;
Select count(*) from users_ids;