laravel function ::with is not generating the correct sql - laravel

I have the following query in Laravel 4:
Tasks::with('tasks_status')
and generate the following sqls:
select * from `tasks`
select * from `tasks_status` where `tasks_status`.`status` in (2, 0)
The connection in models is well done.
The "tasks_status" table have values from 1 to 7 (as id's).
The "tasks" table return values for statuses from 1 to 7.
The second sql shouldn't be?
select * from tasks_status where tasks_status.status in (1,2,3,4,5,6,7)
Thank you.

The problem is solved. I made the incorrect keys relations.
Thank you for your time.

Related

How to use Nested functions substr(), cast() and Max() in query builder in laravel 8?

I posted this question yesterday but I think it's unclear so I deleted it and posted it again with more details.
In my oracle database I have a USERS table with id_user defined as varchar and this varchar is like this: '145/1' ...... '145/9' so to add a new user I check the maximum value ('145/9') and add 1 to the second part of id_user (after the slash) so that the id_user is '145/10'.
The steps are like this:
First: I'm using substr() to get the second part (after the slash) of all id_user.
Second: I use Cast() to convert it to Int.
Third: I use Max() to get the maximum value of Int numbers.
Finally in my laravel code I use the result of this query(the result is 9) and add 1 to it and insert a new user in the users table with id_user = '145/10' and so on.
This query works fine but I need it in the Query Builder so I am trying a lot of queries but they didn't work.(please help me)
SELECT MAX(CAST(SUBSTR(id_user, INSTR (id_user, '/') + 1) AS INT)) AS Aggregate
FROM "users"
WHERE "ID_USER" LIKE '145/%';
Finally, this query gives me the correct maximum:
DB::table('users')->select(DB::raw('MAX(CAST(SUBSTR(id_user,INSTR(id_user, \'/\') + 1) AS INT)) as max')) ->where('id_user','like','145'.'/%')->get()[0];

select and calculate numdays from two tables in oracle

I m doing a leave calculation. I have Leave requested table and Employee Table.
Their relationship is Employee can request many Leaves. i.e
Leave request table has Employee_Serail_ID as one to many. I have done the following query to select all leave request and calculkate the number of days.
SELECT (LR.DATE_TO - LR.DATE_FROM) as NumDays ,
LR.EMPLOYEE_SERIAL_ID, LR.ID as LEAVE_REQUEST_ID
FROM TBL_LEAVE_REQUEST LR ;
NUMDAYS EMPLOYEE_SERIAL_ID LEAVE_REQUEST_ID
3 EMP_286 LEAVE_35
2 EMP_243 LEAVE_36
2 EMP_284 LEAVE_37
3 EMP_243 LEAVE_38
32 EMP_243 LEAVE_39
0 EMP_303 LEAVE_40
1 EMP_241 LEAVE_41
But , i figured out that all employee who have not requested leave will not be selected using this query.
I want to modify this query that - if the employee has rquested a leave it will show the numdays , and if it has not this query should return Numdays 0 for all employees.
Numdays
You'll need to left join your leave request table to your actual employee table. This will give you an employee record, even if they don't have a leave request.
Since you haven't posted your schema, and you haven't specified what database you're actually using, I can't write much of the query for you. Your logic will look something like this:
SELECT
T.EMPLOYEE_ID
, ISNULL((LR.DATE_TO - LR.DATE_FROM), 0) as NumDays
, LR.EMPLOYEE_SERIAL_ID
, LR.ID as LEAVE_REQUEST_ID
FROM
TBL_EMPLOYEE T
LEFT JOIN TBL_LEAVE_REQUEST LR
on T.EMPLOYEE_ID = LR.EMPLOYEE_ID
;
The ISNULL function is used by MSSQL Server. Other databases require different functions.
If you're using Oracle, replace ISNULL( with NVL(.
If you're using PostgreSQL or MySQL, you'll want the command COALESCE(.
A note in ISNULL() and NVL() vs COALESCE(). As #Ronnis pointed out, any ANSI compliant database should support the COALESCE() function.
Looking into the documentation a little further, you may get better query performance using COALESCE() than NVL() or ISNULL(). The former will short circuit its evaluation, whereas the other two will not.

Oracle SELECT * FROM LARGE_TABLE - takes minutes to respond

So I have a simple table with 5 or so columns, one of which is a clob containing some JSON data.
I am running
SELECT * FROM BIG_TABLE
SELECT * FROM BIG_TABLE WHERE ROWNUM < 2
SELECT * FROM BIG_TABLE WHERE ROWNUM = 1
SELECT * FROM BIG_TABLE WHERE ID=x
I expect that any fractionally intelligent relational database would return the data immediately. We are not imposing order by/group by clauses, so why not return the data as and when you find it?
Of all the forms of SELECT statements above, only 4. returned in a sub-second manner. This is unexpected for 1-3 which are returning between 1 and 10 minutes before the query shows any responses in SQL Developer. SQL Developer has the standard SQL Array Fetch Size of 50 (JDBC Fetch size of 50 rows) so at a minimum, it is taking 1-10 minutes to return 50 rows from a simple table with no joins on a super high-performance RAC cluster backed by fancy 4-tiered EMC disk subsystem.
Explain plans show a table scan. Fine, but why should I wait 1-10 minutes for the results with rownum in the WHERE clause?
What is going on here?
OK - I found the issue. ROWNUM does not operate like I thought it did and in the code above it never stops the full table scan.
This is because:
RowNum is assigned during the predicate operation (where clause evaluation) and incremented afterwards, i.e.: your row makes it into the result set and then gets rownum assigned.
In order to filter by rownum you need to already have it exist, something like ...
SELECT * FROM (SELECT * FROM BIG_TABLE) WHERE ROWNUM < 1
In effect what this means is that there is no way to filter out the top 5 rows from a table without having first selected the entire table if no other filter criteria are involved.
I solved my problem like this...
SELECT * FROM (SELECT * FROM BIG_TABLE WHERE
DATE_COL BETWEEN :Date1 AND :Date2) WHERE ROWNUM < :x;

Comparing Similar Hive Tables

I have two hive tables (t1 and t2) that I would like to compare. The second table has 5 additional columns that are not in the first table. Other than the five disjoint fields, the two tables should be identical. I am trying to write a query to check this. Here is what I have so far:
SELECT * FROM t1
UNION ALL
select * from t2
GROUP BY some_value
HAVING count(*) == 2
If the tables are identical, this should return 0 records. However, since the second table contains 5 extra fields, I need to change the second select statement to reflect this. There are almost 60 column names so I would really hate to write it like this:
SELECT * FROM t1
UNION ALL
select field1, field2, field3,...,fieldn from t2
GROUP BY some_value
HAVING count(*) == 2
I have looked around and I know there is no select * EXCEPT syntax, but is there a way to do this query without having to explicity name each column that I want included in the final result?
You should have used UNION DISTINCT for the logic you are applying.
However, the number and names of columns returned by each select_statement have to be the same otherwise a schema error is thrown.
You could have a look at this Python program that handles such comparisons of Hive tables (comparing all the rows and all the columns), and would show you in a webpage the differences that might appear: https://github.com/bolcom/hive_compared_bq
To skip the 5 extra fields, you could use the "--ignore-columns" option.

Update One table Column with Values from Another table Having Similar

Hi Guys I have Two tables (MIGADM.CORPMISCELLANEOUSINFO and CRMUSER.PREFERENCES) and Each Has a field called PREFERENCE_ID and ORGKEY. I want to Update the Preference ID for MIGADM.CORPMISCELLANEOUSINFO with Preference_ID from CRMUSER.PREFERENCES for Each Corresponding ORGKEY. SO I wrote this Query;
update migadm.CORPMISCELLANEOUSINFO s set s.PREFERENCE_ID = (
select e.PREFERENCE_ID from crmuser.preferences e where s.ORGKEY = e.ORGKEY)
But I get:
ORA-01427: single-row subquery returns more than one row
What Should I do?
It means the columns you have selected are not unique enough to identify one row in your source table. Your first step would be to identify those columns.
To see the set of rows that have this problem, run this query.
select e.origkey,
count(*)
from crmuser.preferences e
group by e.origkey
having count(*) > 1
eg : for origkey of 2, let's say there are two rows in the preferences table.
orig_key PREFERENCE_ID
2 202
2 201
Oracle is not sure which of these should be used to update the preference_id column in CORPMISCELLANEOUSINFO
identify the row where the subquery returns more than one row (You could use REJECT ERROR clause to do it for instance) or use the condition 'where rownum = 1'.

Resources