Merging similar tables in SQL part 2 - php

I think people were reluctant to help me last time because I didn't want to post up the columns, but I'm eager to solve this, so I'm putting aside my better judgement and laying it bare.
OK, I have two similar tables, and I'm using MySQL, and PHP to generate the tables. So far I have 3 tables:
CPE:
fastethernet00,subnet00,fastethernet01,subnet01,hsrp,vip,bgp,bgp_neighbor,remote_as,bgp_customer_net,next_hop,loopback,cpe,hostname,cpe_cust_index,int_next_hop_1,int_next_hop_2,voice,solution
Liverpool:
apn1,apn2,apn3,apn4,apn5,customer,vpi_vci,vlan1,cust_index,RADIUS,nexthop,atinterface1,atinterface2,feinterface1,feinterface2,spinterface1,spinterface2,ip_address_range1,ip_address_range2,handset_address_range1,handset_address_range2,handset_address_range3,handset_address_range4,handset_address_range5,handset_address_range6,handset_address_range7,handset_address_range8,handset_address_range9,handset_address_range10,handset_address_range11,handset_address_range12,handset_address_range13,handset_address_range14,handset_address_range15,handset_address_range16,DNS_Server1,DNS_Server2,OLVDMVPN,live
Greenwich:
apn_1,apn_2,apn_3,apn_4,apn_5,customer_,vpi_vci_,vlan_1,cust_index_,RADIUS_,nexthop_,atinterface_1,atinterface_2,feinterface_1,feinterface_2,spinterface_1,spinterface_2,ip_address_range_1,ip_address_range_2,handset_address_range_1,handset_address_range_2,handset_address_range_3,handset_address_range_4,handset_address_range_5,handset_address_range_6,handset_address_range_7,handset_address_range_8,handset_address_range_9,handset_address_range_10,handset_address_range_11,handset_address_range_12,handset_address_range_13,handset_address_range_14,handset_address_range_15,handset_address_range_16,DNS_Server_1,DNS_Server_2,OLVDMVPN_,live_
The first step I need to accomplish is linking Greenwich to Liverpool via apn_1/apn1. The same record won't necessarily be on both, and there may even be duplicates. The php I'm using to create this table is throwing up a syntax error around the UNION ALL:
mysql_query ("CREATE TABLE Both (
SELECT * FROM `Greenwich` left outer join Liverpool on Greenwich.apn_1 = Liverpool.apn1
UNION ALL
SELECT * FROM Greenwich
RIGHT OUTER JOIN Liverpool on Greenwich.apn_1 = Liverpool.apn1 where Greenwich.apn_1 is null)")
Can anyone see what's wrong with that?
Afterwards, I need to then link the resultant table to cpe_cust_index via cust_index/cust_index_, so ideally I think I need to merge these cells. If these cells are present in Greenwich and Liverpool they will always be the same.
Thanks in advance!

The answer is the 3rd way described here, using a "mutex (mutually exclusive) table
http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/
This plus a filter give me exactly what I need. Now I just have to merge the index fields together, and apn 1 fields together.

Try naming all the columns that you want to select and alias then using AS so that they match. Union does not like column name mismatches.
Eg.
Select col1 as col1, col2 as col2 from table1
Union
Select col-1 as col1, col-2 as col2 from table2

Related

check combination from a collection exists in a table

One of my db table has columns COLUMN1,COLUMN2,COLUMN3 which hold ids of users. This group of 3 should be unique. Mean stu1, stu2, stu3 record exists in above columns, it cannot have stu2, stu1, stu3 again. But it can have stu1, stu2, stu4
and and I have an array of students i.e [stu1, stu2, stu3, ....... stuN]
N could go upto 5000
What is the most efficient approach to find a combination from the array, which is not a duplicate.
I am using mysql and php (latest versions)
As #Strawberry noted in their comment, the design of the table (and the necessity of this query) seems suspicious. But assuming that it has to be this way, there's a couple of ways to go about this. A nice and clean trick would be to leverage the ordering capabilities of JSON_EXTRACT():
SELECT t2.ids, COUNT(1) occurrences
FROM (
SELECT
t.col1,
t.col2,
t.col3,
JSON_EXTRACT(JSON_OBJECT(col1, col1, col2, col2, col3, col3), '$.*') ids
FROM test t
) t2
GROUP BY t2.ids
Here's a working example. The idea here is that we create a JSON object containing the three ids (as both keys and values), and when we extract it again it will be ordered by key. Then we can just group by and count.

joining two tables with same id and same name of columns but different values

I am trying to do what I just said. I have two joined tables. They are joined by same id's. But There are two columns with same name and I only want to output one of the columns.
$query= "SELECT *
FROM users
INNER JOIN bookings ON users.username = bookings.personal
JOIN products ON bookings.productid = products.productid
WHERE users.id = $id
";
$selectproductname = $db->prepare($selectuseremailquery);
$selectproductname ->execute(array());
foreach($selectproductname as $index => $rs) :
it gives me something like this table:
-id -----id----products----products------
- -
-1 1 a cup coffee cup -
-2 2 a pen a purple pen -
-----------------------------------------
I want to output the coffee cup, but there are two columns with same name. How can I output?
This didn't work:
<td><div> <?php echo $rs['products.productname']; ?> </div> </td>
And I have to select all of it with *.
Using SELECT * is generally considered harmful in production software, especially in php, for the precise reason you're asking about.
In php, sometimes the result set is loaded into an associative array. That will cause data from all but one of each set of duplicate column names to disappear.
You want to use
SELECT products.id,
products.productname,
products.id AS product_id,
bookings.*,
etc., etc.
to enumerate the columns you need in your result set. (Notice that I'm guessing at your column names).
I know your question says you have to use SELECT *. I doubt that's true. If so it's a likely to be requirement imposed by somebody who doesn't know what they're talking about. (Stupid professor tricks come to mind.)
If you do have to use SELECT *, you'll need to use the result set metadata to examine each column's metadata and figure out which columns you need to extract. getColumnMeta() does that.

How get table with references other table in yii2?

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

Query Multiple table with same structure

I have a few tables with the same structure built and wanted to query them at once using PHP MYSQL.
The challenging part that I face was to query x tables where I do not know how many of them there. (Assuming user will select the tables on UI perspective). The reason of this is to output them into a csv format.
For example:-
Date, xTable, yTable, zTable, ....
==============================b
bla 1 2 3
bla 4 3 5
...
...
How can I modify below query to cater for above needs? (The query needs to be flexible enough to query multi table).
SELECT created_dt,
xTable FROM needToQueryMultiTable WHERE created_dt BETWEEN ? AND ?
You can create a view of all possible tables:
create v_tables as
select 'table1' as which, t.*
from table1 t
union all
select 'table2' as which, t.*
from table2 t
. . .;
Then, you can select from the view.
In MySQL this is not particularly efficient, because all the tables will still need to be read. However, for smaller tables this could be a reasonable solution.
You should ask yourself why you have identically structured tables in the databases. In general, these should be combined into a single table. For performance, you can partition the table.

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).

Categories