How to Get Context of third phase table in entity framework - entity-framework-4.1

I have three Tables
table1:
table1id (pk),
table2id (fk)
table2:
table2id(pk),
table3id(fk)
table3:
table3id(pk)
Name
Can I get the value of a particular Name from table3 using Table1 context in Entity framework?
i.e if I have table1id from that I can get table2id from there context of table3 is it possible to get it?
If yes, how to use it?
Can anyone please give me a way to use it..
Note : I don't want to use any Stored Procedure for it.

If you have mapped the navigational properties correctly you can do that as follows
var t3s = context.Table3s
.Where(t3 => t3.Table2s.Any(t2 => t2.Table1s.Any(t1 => t1.table1id == "foo")))
.ToList();

Related

How to save a record with multiples foreing keys in Larave 8?

I have these tables:
vehicle_brands:
id
name
engines:
id
name
vehicles:
id
name
vehicle_brand_id
engine_id
vehicles depends of vehicle_brands and engines, I want save vehicle_brands and engines id when I save a vehicle record, I would do this but I think this isn't the best approach
$vehicleBrand = VehicleBrand::find(1);
$engine = Engine::find(1);
$vehicle = Vehicle::create[(...)];
$vehicleBrand->vehicles()->save($vehicles);
$engine->vehicles()->save($vehicles);
Why not set it directly during creation:
$vehicle = Vehicle::create([
"vehicle_brand_id" => 1,
"engine_id" => 1
]);
No need to look up your brand and engine of you already know the IDs. Make sure the values above are marked as $fillable

mysql extract_json value from array

I have a MySQL DB containing a table named "products".
This table contains a Json Data_type column named "values".
I would like to find the path to extract a specific value :
select JSON_EXTRACT(values, '$.COD')
from products
where id = '1'
returns :
"COD": {"<channels>": {"<locales>": "3699999999999"}}
And what I want is "3699999999999".
It is pretty obvious that my path is not the good one, but I can't find the solution.
Thanks for your help !
You can try
SELECT JSON_EXTRACT(`values`, '$.COD.<channels>.<locales>') FROM products WHERE id = '1';
For more details please refer mysql-for-your-json

How to get a correct simple Spring data JPA query which returns map?

Map<id,HelloEntity > = helloEntityRepository.getval( List<integer> id);
I am try to creating getting the data like this:
#Query(value="Select (??) FROM HelloEntitytable WHERE id =:IN(Id)")
Map<id,HelloEntity >getmatchingidValues(#Param("Id")List<integer> id);
This is fix that for every id have only one entity to map. What is the correct query for above situation?
Where id is one parameter in HelloEntitytable. Like there are following column in table:
generatedid (Primary Key), id (Integer), name (String).

How to get the last updated row using a single query

I searched enough before posting it.
My table structure:
aid | bid | cid | did |
Where aid, bid together are the primary keys.
When I update the value of cid using a where clause for aid, bid I also want to get the did value of the updated row.
Something like this:
$this->db->set('cid', 1, FALSE)
->where(array(
'aid' => $a_id,
'bid' => $b_id
))
->update('my_table')
->select('did');
The above query says:
Fatal error: Call to a member function select() on a non-object in...
I tried this:
How to get ID of the last updated row in MySQL?
Which is like 3 queries.
I'd suggest fetching the values you're about to update, store their IDs in an array, and run an UPDATE with a WHERE id IN (1, 2, ...).
What you're trying to do is not supported by MySQL. You'll need to run at least 2 queries, and since you're fetching the values the first time and already know what values you're updating, then you can also recreate the new row and it's values without using a query after UPDATE.
In your given example:
$this->db->set('cid', 1, FALSE)
->where(array(
'aid' => $a_id,
'bid' => $b_id
))
->update('my_table')
->select('did');
set(), where() and also select() returns an object that builds on the query. However update() return a value which is the results and doesn't have a function called select() and not set() and where() for that matter.

Query Multiple Rows in the same Table for one Variable

I have a table setup like this:
session_id | event_id | moderator | speaker_1 | speaker_2 | speaker_3 | ...keeps going to speaker_10
What I am trying to do is setup a query that searches for 1 variable "speakerid = 13245" and check rows
'moderator', 'speaker_1', 'speaker_2', 'speaker_3', 'speaker_4', 'speaker_5', 'speaker_6', 'speaker_7', 'speaker_8', 'speaker_9', 'speaker_10'
Then return every 'session_id' corresponding to any row that contains speakerid = 12345 in any of the 11 speaker rows.
I know it has something to do with an INNER JOIN but after a lot of searching I can't find anything specific enough. I've been following stackoverflow for years now and this is my first ever post.
It really sounds like you need to normalize this table and have a table of sessions/events and a table of speakers related to it through a third sesssions_speaker table. That way you don't need to change your table schema when you have an event with 12+ speakers.
That being said, you can query like this to get the result you need
SELECT session_id
FROM table
WHERE
moderator = ?
OR speaker_1 = ?
OR speaker_2 = ?
...
OR speaker_11 = ?
I think you just need to use LIKE with OR to return the rows where any field contains "speakerid = 12345":
SELECT Session_Id
FROM YourTable
WHERE Moderator Like '%speakerid = 13245%'
OR speaker_1 Like '%speakerid = 13245%'
OR ...
You should read up on database normalization as speaker_n columns are a bad sign. You probably want a Speakers table amd a "Session-Speakers" mapping table. This would certainly make your query easier, but for now you have no choice but to search all columns:
SELECT sesion_id FROM t1 WHERE
moderator = '12345'
OR speaker_1 = 12345
etc.
You can do this using in in the where clause:
select session_id
from t
where 13245 in (moderator, speaker_1, speaker_2, speaker_3, speaker_4,
speaker_5, speaker_6, speaker_7, speaker_8, speaker_9,
speaker_10)