Laravel query builder select all except specific column - laravel

I have 2 tables with the same columns except the second table have one more column, this column is foreign key of the first;
what i want is to make union query;but for union the column must the same; so i want to select all column except for the column distinct;
The easy way is to provide in select all the same column:
$a = Table1::select(['column1', 'column2', 'etc...']);
$b = Table2::select(['column1', 'column2', 'etc...']);
and go with $a->union($b)->get();
but if i have too much column, i end up with so much column to provide in the select function; so what i want is to provide in the query the column that i don't want to retrieve;
i can put protected $hidden in the second table model but for some reason i need this distinct column on some other query;

Get all columns name by Schema::getColumnListing($table);
And computes the intersection of arrays
array_intersect($columnsName1, $columnsName2);

I haven't done it with 2 tables but I am using collections and rejecting certain columns on a similar project:-
$row = Table1::firstOrFail();
$exclude=['column_1', 'column_2'];
return collect(array_keys($row->getAttributes()))
->reject(function ($name) use ($row, $exclude) {
return in_array($name, $row->getHidden())
|| in_array($name, $exclude);
}
);

Related

How to store Condition in a column in Database

I want to store condition in database.
Each row of a certain table contains a specific condition involving its columns
Ex,.
table_name
value_x
value_y
condition
result
Then for querying
$condition = TableName::find($id)->condtion;
$resultSet = TableName::whereRaw($condition)->get();
I know this code is silly, but i hope you get the point.

How to count for a substring over two tables in codeigniter

I have two tables
table1
id|name|next_field
table2
id|id_of_table1|whatelse
I do a msql query to get all entries of table1 and the number of entries in table2 who has table2.id_of_table1 = table1.id
This is my query - it works fine.
$select =array('table1.*', 'COUNT(table2.id) AS `my_count_result`',);
$this->db->select($select);
if($id!=false){ $this->db->where('id',$id); }
$this->db->from('table1 as t1');
$this->db->join('table2 as t2', 't1.id = t2.id_of_table1');
return $this->db->get()->result_array();
Now I have another field which has coma-separated data
table1.next_field = info1, info2, next,...
Now I want to check in the same way like the first query how often for example "info2" is as a part inside the table1.next_field
Is it possible, how?
After all i decide to change my database structure to make the work and handle much easier.

Laravel where clause of current and related table

how to compare current table column to related table column
example: A.quantity < B.criticalQuantity
similarly like this AModel::where('quantity', "<", "b.criticalQuantity")->get()
the relations is
B HasMany A
A BelongsTo B
you can use whereColumn
it is specialist in comparing columns not a column with value.
anyway you can't directly compare two columns from two table, you have to join them first by anyway of join types
something like:
$values = ModelA::join('model_b_table_name', 'model_b_table_name.id', 'model_a_table_name.model_b_id')
->whereColumn('model_b_table_name.column.quantity', 'model_b_table_name.quantity')
->get();
you must be specific in joining the table, you should join by the columns that consist the relation between the two tables.
->whereRaw('table_1.name = table_2.name')

select data from multiple tales using linq query

I have 2 tables I want to select all records from table one but in second table i want select all records that's on the id base that id is not related to table one .i want to do this in linq query .
any help
For what I could understand, you want to do a join between the two tables:
var query = database.FirstTable // starting point - the first table from your question
.Join(database.SecondTable, // the second table
first => first.ID, // Select the primary key (the first part of the "on" clause in an sql "join" statement)
second => second.first_ID, // Select the foreign key (the second part of the "on" clause)
(first, second) => new { First = first, Second = second }) // selection
.Where(result => result.first.ID == id); // where statement

creating a hiveQL query that uses UDF function that can return column names

I want to create a Hive UDF function that returns specific column names based on some value say retreivecol(age).If the age is 20 then return the list of column names to be used in select query like 'name,email,fbuserid,friend list ' etc and if the age is less than 20 return 'name' alone.So I want my HIVE QL query to look like
select retreivecol(age) from User_Data;
The above query just prints the name of the columns like 'name,email,fbuserid,friendslist' etc as opposed to treating them as column names and filtering based on the same.Any pointers are appreciated.
I'm not sure a UDF is the right place to do this, as UDF's simply see the value passed to them, they don't really have access to the whole table structure.
Instead, could you do this in a nested table?
select name,email,id FROM
(
select
name,
if(age < 20, email, NULL) as email,
if(age < 20, id, NULL) as id
FROM mytable
) a

Resources