PhpMyAdmin Merging two tables - mysql

I have Table one and Table two. Table one has columns zoe and clo, but table two has oez and olc and it goes like this zoe=oez and cloe=olc how can I merge them?

I'm not sure if this is what you're looking for, but using union all can merge the results into as single table. You would have to, however, create a new primary key if this is going to be a permanent table.
Select a.id as id, a.value as value
From a
UNION ALL
Select b.id1 as id, b.value1 as value
From b
Group by id, value
Example:
http://www.sqlfiddle.com/#!2/093e1/4

Related

get column value with ID as foreign key in recursion

I have created a temp table from recursion (CTE), then left join PARAM_VALUE from another table. My temp table look like this
I have the BOQ_ITEM_FK which points to ID having the PARAM_VALUE. I want to get the PARAM_VALUE from the parent ID and apply it to the child PARAM_VALUE.
What I have tried:
SELECT ID, BRIEF, REFERENCE, PARAM_VALUE
FROM #BOQ_TABLE
WHERE ID IN (SELECT BOQ_ITEM_FK FROM #BOQ_TABLE)
But I only get:
Thanks in advance
In order to get the PARAM_VALUE from another related row in the same table, you'll need to join the table with itself.
When a query includes the same table multiple times you need to assign an alias to each table instance, in order to identify where each column is coming from. I chose the aliases a and b but you are free to choose any alias that makes sense to you.
Your query could look like:
select
a.id,
a.brief,
a.reference,
b.param_value
from #boq_table a
left join #boq_table b on b.id = a.boq_item_fk
Notice that:
The PARAM_VALUE column is coming from the second table instance and, therefore, comes from a different row.
The query uses an outer join (LEFT JOIN) in case the parent row does not exist.

Find the row that doesn't exists in the table

In my table, there are values for poster_display_no 1 and 3 but not 2. I want to fetch the poster_display_no that doesnt exists in the table. The below query is not working as expected. Any idea what is wrong in the above query?
select `poster_display_no` as missing_num
from `poster-judging-app`.poster_details
where `poster_display_no` not in (1,2,3)
This is kind of a hack, but you could build a derived table with the list of poster_display_nos that you want to chek for, left join with your table and filter on the missing ones:
select t.poster_display_no
from (
select 1 poster_display_no
union all select 2
union all select 3
) t
left join poster_details p on p.poster_display_no = t.poster_display_no
where p.poster_display_no is null
Another, more scalable option would be to create a separate referential table to store the list of poster_display_nos that you want to check for, and then bring that table directly to the query.

Create Table3 AS SELECT WHERE id from table1 = id from table2

What I have tried:
CREATE TABLE temporary_bundle_wired
AS SELECT * FROM temporary_bundle,wired_items
WHERE temporary_bundle.id = wired_items.id;
I have two exisiting tables:
temporary_bundle
wired_items
I want to create a third table, called temporary_bundle_wired.
In this table I want to insert all rows (and their columns and fields) from temporary_bundle WHERE temporary_bundle.id = wired_items.id
I also want to delete those records from temporary_bundle once I have moved them into tempoary_bundle_wired.
The Query I have tried returns:
duplicate column name Id
The Query I have tried returns:
duplicate column name Id
The above error blatantly indicates that you have duplicate columns. The column names (id) in the new table you are creating are clashing between two old tables' common attributes (temporary_bundle.id and wired_items.id)
Make sure there are no other common attributes between two tables. If present then alias them from either of the table before inserting.
Try this if others are not working.
CREATE TABLE temporary_bundle_wired
AS
SELECT
t.id as id, user_id, base_item, extra_data, x, y, z, rot, wall_pos, limited_number, limited_stack, sandbox -- and all other attributes you want
FROM temporary_bundle t, wired_items w
WHERE t.id = w.id;
DELETING -
This is a different query all together.
DELETE from temporary_bundle t WHERE t.id = (select id from wired_items);
The problem is that both tables have a column named id, and your are selecting the two for creating the new table.
You will have to specify each column of one of the tables, so you can rename the id column:
CREATE TABLE temporary_bundle_wired
AS SELECT a.id as 'othername', user_id, base_item, ..., b.*
FROM temporary_bundle a, wired_items b
WHERE temporary_bundle.id = wired_items.id;
Cheers.
One problem is indeed that id is the same between the two tables. Other columns may be as well, but if id is the only problem then use the using clause:
CREATE TABLE temporary_bundle_wired AS
SELECT *
FROM temporary_bundle JOIN
wired_items
USING (id);

select from database table rows that are not in another table

I have a table of 'entries' in a MYSQL database. I have another table that records activity on those entries, with the id of the entry as a foreign key. I want to select from my first table entries that do not appear in the second table.
How can I use SQL to make this happen? Do I have to iterate through both tables and compare every entry with every other entry? Is there an easier way to do this?
ex. I have a table with an entry data column and a user name column. I have another table with an entry id column and a user id column. I want to select from my first table all of the entries which do not appear in the second table with a given user id.
Thanks ahead of time. I have been struggling with this experiment for a while. I imagine I have to join the two tables somehow?
Several ways to achieve this, NOT IN, NOT EXISTS, LEFT JOIN / NULL check. Here's one with NOT EXISTS:
SELECT *
FROM FirstTable T
WHERE NOT EXISTS (
SELECT *
FROM SecondTable T2
WHERE T.Id = T2.Id
)
From what I understand, you want to select all rows where the foreign key doesn't match anything in the other table. This should do the trick:
SELECT *
FROM Data A
RIGHT JOIN Entry B
ON A.ID = B.ID
WHERE A.ID IS NULL
Here's a handy chart that illustrates how to use joins for stuff like this.
You can also use NOT IN, and the mechanics for this one are actually a bit easier to understand.
SELECT *
FROM Data A
WHERE A.ID NOT IN (SELECT ID FROM Entry)

Select multiple columns from one alias

In my schema, there are two tables with the same column names (Changing them is not an option)
Performing a query with
select * from tabA join tabB results in a mysql error of 'Duplicate column name col'
The only solution to this is to select using aliases, however I do not want to type alias.col for every column (since I need all columns from both tables)
Is it possible to do something along the lines of:
select tabA.(colA, colB, colC...), tabB.(colA, colB, colC...)
no its not possible.
you have to do like that
select tabA.colA, tabA.colB, tabA.colC..., tabB.colA, tabB.colB, tabB.colC...
if you have same name column in both tables , just give it an other alias like that.
lets say you have id column name in both tables.
select tabA.id , tabB.id as id_B
will give you result
id id_B
Linger's fiddle