Get Single record for Each Ids in Laravel - php

How to get Single Record (Last Inserted Record) for Selected Ids in Laravel.
My Query :
$familymembers=Membersfamilydetails::Where('member_id',$id)->select('id')->get();
foreach($familymembers as $famil){
$faids[]=$famil->id;
}
$familyeducationDatas=Familyeducation::whereIn('familymemberid', $faids)->orderBy('id','DESC')->first();
If Family Members Table Return Four Ids, I Need Last Inserted Record for this Four Ids in Family Education Table.

You should use a column
created_at
which should be the timestamp in Family Education table and re-write the query as follows:
$familyeducationDatas=Familyeducation::whereIn('familymemberid',
$faids)->orderBy('created_at','DESC')->first();

Use SELECT MAX(id) FROM TABLE. This will return the highest currently in use primary key for the table.

Related

Store average data along with the table name into another table using mysql

I want to store the average values from one table to another table.And also I need to store the table name in another column along with its average data using mysql.Here below is the table I need to have.
Need to add test table's average values into "avg table" along with the test table name as a test id for the second table
I dont know how to add tables in here.
Test 1 table
AvgData table
I think you want to aggregate by date and then take the averages of the temperature and humidity columns:
SELECT
DATE(Time) AS Date,
AVG(Temperature) AS Avg_Temperature,
AVG(Humidity) AS Avg_Humidity
FROM Test1
GROUP BY
DATE(Time);

foreign keys mysql php

I have two table in which I need to submit data. It is a directory listing website on the first table has the common data name etc but I have to manage business hours as well look below
**Users**
Users_Id Name Age AddressId .........
----+------+------+-----------
**users_hours**
hours_id Users_Id day day day .........
----+------+------+-----------
I have to make a foreign key for this but the only issue is how will I get the id of the user dynamically?
As both of these rows will be created together.
So you want to get the last insert id of Users table, for getting the last id you need to use the mysql_insert_id() here.
First make a insert query for Users and use $userId = mysql_insert_id().
The $userId now hold the last id of the Users table, so you can now make a second query for users_hours.
More about mysql-insert-id.php

reference auto-increment columns?

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');

MySQL query help to fetch data combining two tables

I have following tables
articles_category
id title sef_title
articles_data
id cat_id title sef_title details
On each table "id" is the primary key and articles_data.cat_id is foreign key of articles_category
I need to fetch one latest article data for each articles category with following data.
articles_category.id
articles_category.title
articles_category.sef_title
articles_data.id
articles_data.cat_id
articles_data.title
articles_data.sef_title
articles_data.details
I tried with following query but it displays first article (oldest entry) rather than latest one.
SELECT
articles_category.id as article_cat_id, articles_category.sef_title as cat_sef_title, articles_category.title as cat_title,
articles_data.id, articles_data.cat_id as listing_cat_id, articles_data.title, articles_data.sef_title, articles_data.details
FROM articles_category, articles_data
WHERE articles_category.id = articles_data.cat_id
GROUP BY articles_data.cat_id
ORDER BY articles_data.id DESC
if it's a one to many relation, try (untested):
SELECT *
FROM articles_category, articles_data
WHERE articles_category.id = articles_data.cat_id
AND articles_data.id in (
SELECT max(articles_data.id)
FROM articles_data GROUP BY cat_id
)
you are not guaranteed a particular row on a GROUP BY
and you should really use a date on your article as the max id is never guaranteed to be the latest article even if you are using autoincrement
ORDER affects the display of records after the GROUP function has been performed. The GROUP function recognizes the first record it sees in its search.
What you need to do is perform 2 queries. The first one should be
SELECT max(articles_data.id), articles_data.cat_id ...
GROUP BY articles_data.cat_id
The second query should fetch the associated records using those resultant primary keys.
A possibility is to create another value such as date_created and place a time stamp there when creating an entry.
Then you would just have to ORDER BY date.

Most efficient way to insert two rows that depend on each other

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.

Categories