How get table with references other table in yii2? - php

i have multiple table that join together and i need one query and get all references too ! is that possible in yii2??
get them in hierarchy array ??
How ???
Is it possible to do not use join???
thanks for your help!!!!

If you created the model classes for each table using Gii and selected to create the relations in the generated models, you can do something like the following.
1) In your Countries model just change the method that declares the relationship with Airports like this:
public function getAirports() {
return $this->hasMany(Airports::className(), ['country_id' => 'id'])->with('airlines');
}
2) When you do the query for the countries and you need to have the related airports, airlines and flightbooked do it like this:
$countries = Countries::find()
->where('something = something_else')
->with('airports')
->with('flightbooked')
->all();
This way you will get all the related models populated with way less queries to the database than using lazy-loading.

I just wanted to give a small suggestion:
As you are maintaining the relations in the tables and if you have generated your code using Gii, then that will generate the joins for you. You can then access any column of any table easily.
But I think UNION may not be an alternative to JOIN.

Maybe u can use union all for this. with this operator, you can concatenate the result sets from multiple queries together, preserving all of the rows from each. Note that a UNION operator (without the ALL keyword) will eliminate any "duplicate" rows which exist in the resultset. The UNION ALL operator preserves all of the rows from each query (and will likely perform better since it doesn't have the overhead of performing the duplicate check and removal operation).
The number of columns and data type of each column must match in each of the queries. If one of the queries has more columns than the other, we sometimes include dummy expressions in the other query to make the columns and datatypes "match". Often, it's helpful to include an expression (an extra column) in the SELECT list of each query that returns a literal, to reveal which of the queries was the "source" of the row.
SELECT 'col1' AS source, col23, col343, col33, d FROM table1 WHERE ...
UNION ALL
SELECT 'col2', t2.fee, table2.fi, table2.fo, 'fum' FROM table2 JOIN table3 ON ...
UNION ALL
SELECT 'col3', '1', '2', buckle, my_shoe FROM table4
You can wrap a query like this in a set of parenthesis, and use it as an inline view (or "derived table", in MySQL lingo), so that you can perform aggregate operations on all of the rows. e.g:
select one.a
, SUM(one.b)
FROM (
SELECT 'q1' AS source, a, b, c, d FROM t1
UNION ALL
SELECT 'q2', t2.fee, t2.fi, t2.fo, 'fum' FROM t2
) one
GROUP BY one.a
ORDER BY one.a
But i think joining tables more suitable. Hope help you

Related

MYSQL/PHP - Select all columns from table records distincting by one column

I'm a bit confused about DISTINCT keyword. Let's guess that this query will get all the records distincting the columns set in the query:
$query = "SELECT DISTINCT name FROM people";
Now, that query is fetching all the records distincting column "name" and at the same time only fetching "name" column. How I'm supposed to ONLY distinct one column and at the same time get all the desired columns?
This would be the scheme:
NEEDED COLUMNS
name
surname
age
DISTINCTING COLUMNS
name
What would be the correct sintaxis for that query? Thanks in advance.
If you want one row per name, then a normal method is an aggregation query:
select name, max(surname) as surname, max(age) as age
from t
group by name;
MySQL supports an extension of the group by, which allows you to write a query such as:
select t.*
from t
group by name;
I strongly recommend that you do not use this. It is non-standard and the values come from indeterminate matching rows. There is not even a guarantee that they come from the same row (although they typically do in practice).
Often, you want something like that biggest age. If so, you handle this differently:
select t.*
from t
where t.age = (select max(t2.age) from t t2 where t2.name = t.name);
Note: This doesn't use group by. And, it will return duplicates if there are multiple rows with the same age.
Another method uses variables -- another MySQL-specific feature. But, if you are still learning about select, you should probably wait to learn about variables.

MySQL - selecting from multiple tables, possibly without joins?

It's been a while since I needed help, but today I'm here to basically get assistance from your knowledge. I'm currently quite stuck on a very annoying SQL problem, which is the following.
I have two tables. Painteditems, and specialitems. Both tables have unique column names (painteditemid, specialitemid etc), yet both tables share similar values. I want to get results from both tables.
Let's say this is my setup:
PaintedItems
paintedItemName
paintedItemColor
visible
SpecialItems
specialItemName
specialItemColor
visible
I used this query:
SELECT *
FROM `painteditems` AS pa,
`specialitems` AS sp
WHERE (pa.`visible` = 1
OR sp.`visible` = 1)
AND (pa.`painteditemname` = 'itemname1'
OR sp.`specialitemname` = 'itemname1')
AND (pa.`painteditemcolor` = 'black'
OR sp.`specialitemcolor` = 'black')
That resulted in:
Showing rows 0 - 29 ( 259,040 total, Query took 39.4352 sec)
even though both tables contain only 10.000 rows altogether. Adding this did nothing:
GROUP BY pa.`painteditemid`, sp.`specialitemid`
Still 260k rows. How should I approach this?
Thank you in advance.
edit: fixed spacing, code blocks
Sure sounds like you want a UNION between the two tables. Right now, you are getting a cartesian product which is why the results are so large:
select *, 'painted' Source
from painteditems
where visible = 1
and painteditemname = 'itemname1'
and painteditemcolor = 'black'
union all
select *, 'special' Source
from specialitems
where visible = 1
and specialitemname = 'itemname1'
and specialitemcolor = 'black'
You will need to replace the SELECT * with your column names. Also the number of columns and datatypes must match in both queries.
UNION ALL will return all rows from both tables, if you only want DISTINCT rows then you will want to use UNION
The UNION operator is used to combine the result-set of two or more SELECT statements. Defiantly You can make use of UNION as shown in the #bluefeet's answer If you meet below conditions.
SELECT statement within the UNION must have the same number of
columns
The columns must also have similar data type
The columns in each SELECT statement must be in the same order.
I would do this with a union all in the subquery:
select *
from ((select paintedItemName as ItemName, paintedItemColor as ItemColor, visible, 'Painted' as which
from painteditems
) union all
(select specialItemName, SpecialItemColor, visible, 'Special' as which
from specialitems
)
) t
where visible = 1 and itemname = 'itemname1' and itemcolor = 'black'
This allows you to have only one set of results. In a union, the column names come from the first subquery, which this renames to more generic names. The reason I prefer this approach is because the where clause does not need to be repeated multiple times -- which can lead to errors and maintenance problems.

PHP MySql: explode and JOIN Combination

I put user id with separate comma in MySql TABLE for best user. Example : 1,2,3 Now i work with PHP explode() function for this result:
$bestuser = explode(',',$bestuser);
i have another MySql TABLE for list of user with this row : id/name/datejoin/birthday ....
now, i need to print name of best user with MySql JOIN Methods. actually my mean how to combination explode result with other MySql TABLE result.
NOTE: i know this design(1,2,3) is bad, But I have no choice.
You could write an SQL query to do this:
SELECT id,name
FROM user
WHERE id IN (:yourListOfIds)
Be cautious of SQL injection if the list is at any way user supplied.
See this question, but if you look at the comments on the manual you'll find lots of people talking about exploding.
One can use MySQL's FIND_IN_SET() function in the join criterion:
table_a JOIN table_b ON FIND_IN_SET(table_a.id_a, table_b.csv_a)
However (per the warnings in my comments above) this operation will be terribly inefficient, as MySQL must fully scan both tables.
A much better solution would be to create a table of relations:
CREATE TABLE relations (
FOREIGN KEY (id_a) REFERENCES table_a (id_a),
FOREIGN KEY (id_b) REFERENCES table_b (id_b)
) SELECT table_a.id_a, table_b.id_b
FROM table_a JOIN table_b
ON FIND_IN_SET(table_a.id_a, table_b.csv_a);
ALTER TABLE table_b DROP csv_a;
Then one can query for required data by joining the tables as required:
SELECT table_a.*
FROM table_a JOIN relations USING (id_a)
WHERE relations.id_b = ?
If so desired, one could even use MySQL's GROUP_CONCAT() function to obtain the original CSV:
SELECT table_b.id_b, GROUP_CONCAT(relations.id_a) AS csv_a
FROM table_b JOIN relations USING (id_b)
WHERE ...
GROUP BY table_b.id_b

How to create pagination that based on two sql queries? (PHP)

I have page that display information from two different tables , and for that I have two queries.
There is no related info between these two tables.
Since both queries may contain a lot of information, I need create pagination.
BUT I don't want two separate paginations, I want only one that will contain results from query 1 and query 2 together.
How can I do that?
The only idea I have is to fetch all info of both queries into arrays, then combine the arrays into one, then create pagination that based on that array.
That of course would not help save resources.
You could use a union - the columns you're displaying must line up, so something like this should work:
select
col1 col1_alias,
col2 col2_alias,
...
from
table1
where
...
union
select
col1,
col2,
...
from
table2
where
...
order by col1_alias, col2_alias
limit 10
Basically the union will pull all the data together, and the order by and limit will apply to the whole result set.
The names of the columns don't need to match in the second select, but use column names from the first select for your order by (or create aliases, which is probably more readable depending on your dataset).

See which table a result is in

I took over managing an internal website for the company that I'm working for and I need to get data out of a mysql database. The problem that I'm encountering is that the data is in 6 different tables, all with the same fields but the rows are all unique (the row starts in one table and then gets completely moved to a different table after it is processed by an employee).
Is there an easy way to query against all 6 at once? It would also be useful to be able to retrieve the title of the table it came from.
I'm using PHP to run the query and display it. Would it be better to create another table that defines where all the rows are, have a unique id and then another field for which table it's in?
To complete this query, use union all:
select
'Table1' as TableName,
*
from
Table1
union all
select
'Table2',
*
from
Table2
union all
select
'Table3',
*
from
Table3
...and so on
For better database design, you would want to either have one table with all the rows in it, and a designated Status table where you can link a StatusID column to that says what status that given row is in. A table for each stage in a process is a poor design and will only lead to massive headaches down the road.
If you can't reorganize the tables so that you have just one with all rows and a marker for where in the process they are, I would go for a UNION-approach. Ie:
SELECT 'Data from Table 1', t1.field1, ...
FROM Table1 t1
UNION
SELECT 'Data from Table 2', t2.field1, ...
FROM Table2 t2
UNION
(Table3, 4, 5 and 6 in the same manner)
....
That way you can see where the data is originating from and you get all 6 at once. Just remember that you have to have the exact same field list in all parts of the UNION.
You could create a code generator that generates SQL statements to query the 6 tables. The generator would create a UNION of 6 selects and add a "table" column to each select with a constant value equal to the name of the table queried. That would make writing the statements easy, though I wouldn't say that writing the generator would be.

Categories