Subquery results more than one values - mysql

I have two tables:
table_people
col_name
col_sex
table_gender
col_male
col_female
Suppose the table_people consist three rows, (a,'M'),(b,'M'),(c,'F').
Now I need a query(subquery) to insert this first table value in second table as:
(a,c),(b,'').
If it is possible in mysql?

You table structure is bad.
My suggestion is to drop table_gender because it doesn't make sense at all. You already have a list of person with gender on table table_people.
You can generate a VIEW, if you want to list the gender separately.
CREATE VIEW MaleList
AS
SELECT col_name
FROM table_people
WHERE col_sex = 'M'
and another view for list of females only.
CREATE VIEW FemaleList
AS
SELECT col_name
FROM   table_people
WHERE  col_sex = 'F'

First of all the table structure is bad. I dont see a point of saving M or F in another table when you col_sex in your table_people. Still if you want to do it, first you have to specify a foreign key connecting the two tables.

Related

What is the new table name after joining two tables in sql?

I read the previous posts but I couldn't find one that answered my question.
What would be the name of the table that is made by joining two tables? The reason why I need the name is because I would like to change the column name of the new table using the ALTER TABLE (Table name) RENAME COLUMN (A) to (B). If there is no specified name, how can I name the new table?
ex
SELECT
client_id
,last_name
FROM INDIVIDUAL_CLIENT
UNION ALL
SELECT
client_id
,bus_name
FROM BUSINESS_CLIENT;
I would like to rename the column to last_name/bus_name instead of last_name
In the case of a query what temporal table a query might create internally is not relevant, because you a making a query and getting data back, it doesn't stay in the database as a table, there is no such table. Unless we make it.
if You want TSQL to change a column name it would affect your union query and I base my answer on Your
'I would like to rename the column to last_name/bus_name instead of last_name'
And think this is what you're looking for. Please correct me if it isn't.
In generic SQL what we're doing is putting a label on both projections that are to be displayed in the same column
SELECT
client_id
,last_name [last_name/ bus_name]
FROM INDIVIDUAL_CLIENT
UNION ALL
SELECT
client_id
,bus_name [last_name/ bus_name]
FROM BUSINESS_CLIENT;
update, in MySQL notation uses AS and quotes instead of angle brackets
SELECT
client_id
,last_name as "last_name/ bus_name"
FROM INDIVIDUAL_CLIENT
UNION ALL
SELECT
client_id
,bus_name as "last_name/ bus_name"
FROM BUSINESS_CLIENT;

combobox with data from several fields in access

I have created a table with several columns and a related form. In that form, I have created a combobox to show the columns of the table in differents rows in combobox.
I have 3 fields in my table: product1, product2 and product3 for a same order I have named with a number. When I create the combobox, values show in 3 differents columns in the same row so I can just select the data from the first row and column. But what I need is the field's data displayed in different rows instead showing data in different columns.
I researched in forums, and I read It can be solved with a join query and selecting that in row source in combobox menu, but I have tried it and I got the same result. I don´t know what am I doing wrong. I'd appreciate your help.
Thanks in advance.
You have to pivot the results
this is an example of how to restructure using sub queries bad data designs
SELECT *
FROM
(
SELECT QualifierID -- <--- this is a unique identifier
,
(
SELECT QualifierText
FROM [QDB].[dbo].[Qualifier] x
-- this is how you tie the unique identifier and grab a specific element by itself
-- also pay attention to how we're making it equal to q.QualifierID,
-- which is the alias to the table below
WHERE QualifierID=q.QualifierID
) AS QualifierText
,
(
SELECT [ExampleText]
FROM [QDB].[dbo].[Qualifier] x
-- this is how you tie the unique identifier and grab a specific element by itself
-- also pay attention to how we're making it equal to q.QualifierID,
-- which is the alias to the table below
WHERE QualifierID=q.QualifierID
) AS ExampleText
,
(
SELECT [WhenModified]
FROM [QDB].[dbo].[Qualifier] x
-- this is how you tie the unique identifier and grab a specific element by itself
-- also pay attention to how we're making it equal to q.QualifierID,
-- which is the alias to the table below
WHERE QualifierID=q.QualifierID
) AS WhenModified
FROM [QDB].[dbo].[Qualifier] q -- this is a table alias
) z -- this is the alias fo the subquery combining everything together.

How can I combine these two tables so that I can sort with information on each table, but not get duplicate answers?

I have two tables. The first is named master_list. It has these fields: master_id, item_id, name, img, item_code, and length. My second table is named types_join. It has these fields: master_id and type_id. (There is a third table, but it is not being used in the queries. It is more for reference.) I need to be able to combine these two tables so that I can sift the results to only show certain ones but part of the information to sift is on one table and the other part is on the other one. I don't want duplicate answers.
For example say I only want items that have a type_id of 3 and a length of 18.
When I use
SELECT * FROM master_list LEFT JOIN types_join ON master_list.master_id=types_join.master_id WHERE types_join.type_id = 3 AND master_list.length = 18"
it finds the same thing twice.
How can I query this so I won't get duplicate answers?
Here are the samples from my tables and the result I am getting.
This is what I get with an INNER JOIN:
BTW, master_id and name both only have unique information on the master_list table. However, the types_join table does use the master_id multiple times later on, but not for Lye. That is why I know it is duplicating information.
If you want unique rows from master_list, use exists:
SELECT ml.*
FROM master_list ml
WHERE ml.length = 18 AND
EXISTS (SELECT 1
FROM types_join tj
WHERE ml.master_id = tj.master_id AND tj.type_id = 3
);
Any duplicates you get will be duplicates in master_list. If you want to remove them, you need to provide more information -- I would recommend a new question.
Thank you for the data. But as you can see enter link description here, there is nothing wrong with your query.
Have you tried create an unique index over master_id, just to make sure that you do not have duplicated rows?
CREATE UNIQUE INDEX MyMasterUnique
ON master_list(master_id);

Is there way to add multiple values to 1 ID in access

I have a table that has Act ID, and another table that has Act ID, percentage complete. This can have multiple entries for different days. I need the sum of the percentage added for the Act ID on the first tableZA.108381.080
First table
Act ID Percent Date
ZA.108381.110 Total from 2 table
ZA.108381.120
ZA.108476.020
ZA.108381.110 25% 5/25/19
ZA.108381.110 75 6/1/19
ZA.108381.120
ZA.108476.020
This would be generally considering not good practice. Your primary key should be uniquely identifiable for that specific table, and any other data related to that key should be stored in separate columns.
However since an answer is not a place for a lecture, if you want to store multiple values in you Act ID column, I would suggest changing your primary key to something more generic "RowID". Then using vba to insert multiple values into this field.
However changing the primary key late in a databases life may cause alot of issues or be difficult. So good luck
Storing calculated values in a table has the disadvantage that these entries may become outdated as the other table is updated. It is preferable to query the tables on the fly to always get uptodate results
SELECT A.ActID, SUM(B.Percentage) AS SumPercent
FROM
table1 A
LEFT JOIN table2 B
ON A.ActID = B.ActID
GROUP BY A.ActID
ORDER BY A.ActID
This query allows you to add additional columns from the first table. If you only need the ActID from table 1, then you can simplify the query, and instead take it from table 2:
SELECT ActID, SUM(Percentage) AS SumPercent
FROM table2
GROUP BY ActID
ORDER BY ActID
If you have spaces other other special characters in a column or table name, you must escape it with []. E.g. [Act ID].
Do not change the IDs in the table. If you want to have the result displayed as the ID merged with the sum, change the query to
SELECT A.ActID & "." & Format(SUM(B.Percentage), "0.000") AS Result
FROM ...
See also: SQL GROUP BY Statement (w3schools)

Summing-up For an Arbitrary List of Rows

I wish I could write something like this:
SELECT sum(weight) FROM items WHERE itemID IN (4217,4575,6549,4217)
where itemID is the primary key, and SQLite and MySQL would process it as expected, i.e. add the 4217-th row to the sum twice, but they don't.
is there something at least in MySQL I'm not aware of that's intended for cases like this? if not, what would a workaround be like? any row can have any number of duplicates in the list. the list can be big although in most cases is small.
I don't know any out of the box functionality that can do this. Here is the workaroud. You can insert those values into temp table and use a join:
CREATE TEMPORARY TABLE List(ID INT);
INSERT INTO List(ID) VALUES(4217),(4575),(6549),(4217);
SELECT SUM(i.weight)
FROM items i
JOIN List l ON i.itemID = l.ID;