I'm new to MySQL so I really need some help with an issue I'm facing:
I have 7 tables in same database with some datas from tests:
The 7 tables have different columns but they all have these columns:
name.
second_name.
status.
In status are added current status of each student (accepted or rejected) and I want to display using select the name, second_name from the 7 tables where status = accepted.
I managed to display from 1 table
SELECT name, second_name FROM test1 WHERE status="accepted";
But I can not figure out how to display from all 7.
It will be a real help for me if somebody could give me a hint.
If you do not mind duplicate student names with multiple accepted tests, you can try doing it with UNION ALL:
(SELECT name, second_name FROM test1 WHERE status='accepted')
UNION ALL
(SELECT name, second_name FROM test2 WHERE status='accepted')
UNION ALL
(SELECT name, second_name FROM test3 WHERE status='accepted')
-- ...and so on
IMHO it's better to normalize database to have all the names, secondnames and statuses in the separate table and do the only select instead of UNION to improve performace.
Related
I have this table structure for names_table:
Name
Age
Gender
Someone1
25
Male
Someone2
25
Female
Another table names_with_company has this structure:
CompanyID
Name
Age
Gender
Now, I want to copy the data from names_table by adding a single value to column of CompanyID.
Expected result:
CompanyID
Name
Age
Gender
1234
Someone1
25
Male
1234
Someone2
25
Female
I am quite confused what should I include.
INSERT INTO names_with_company
'1234',SELECT * FROM names_table
or
INSERT INTO names_with_company
SELECT * FROM (
'1234'
UNION
SELECT * FROM names_table
)
These two doesn't work
I know these two tables are two different structures, but is there any way to have a static value in column and rest of the data from another table?
Also, can you please not suggest creating another table and joining them? I prefer it to be done using the above code lines, but with a working logic.
Get into the habbit of always specifying the column names:
INSERT INTO names_with_company (CompanyID, Name, Age, Gender)
SELECT 1234, Name, Age, Gender
FROM names_table;
As you can see, you can provide "literal" values for any column.
you can not able to insert because your 1st query
INSERT INTO names_with_company
'1234',SELECT * FROM names_table
is completely wrong but if you written like below
INSERT INTO names_with_company
SELECT '1234',name,age,gender FROM names_table
it will work
but it is always better to mention the column name explicitly which is given in another answer by #stu
your 2nd query also wrong cause for union operation you have to provider same number of columns for all the selection
INSERT INTO names_with_company
SELECT * FROM (
'1234'
UNION
SELECT * FROM names_table
)
but you have used only one select
write method for union operation is like below
select col1,col2 from table1
union
select col1,col2 from table2
then you can use it any other way
I want to select result from different database based on a subsring value from columns.
Here is my table student:
Original_student Other_student
1010173 1240240
1010173 1240249
The 3rd digit in the number will be used to distinguish database. for example. I want the query be
select original_student, Other_student, month
from student join database-(substring(other_student,3,1).payment
My question is: How can I concatenate the substring to a database name or column name dynamically?
Thanks
Supposing you have a field to identify each student by a unique id (id_student), here is a cheap alternative:
CREATE OR REPLACE VIEW v_student_payment AS
SELECT 0 AS db, payment, id_student FROM database-0
UNION
SELECT 1 AS db, payment, id_student FROM database-1
UNION
SELECT 2 AS db, payment, id_student FROM database-2
UNION
SELECT 3 AS db, payment, id_student FROM database-3
/* here you have to add all databases you're using. There's a little maintenance cost, for if one day there's a new database to be created this view would have to be modified */
;
SELECT
original_student,
Other_student,
month,
v.payment
FROM
student s
JOIN v_student_payment v ON v.id_student = s.id_student AND v.db = SUBSTRING(other_student,3,1)
Did you try using a Case statement for checking with the join. Try looking at this link
Use Case Statement in Join
I have the following result from my SQL query:
EventID P_Num PN_NameCount1 PN_Name
ABC-I-10942683 1089213 1 Company 1
ABC-I-10942683 1326624 8 Company 2
I am still learning this capability in SQL and need some assistance, Pivot's are NOT working in this scenario.
I have tried several different ways in attempting to do this, but was not able to create the desired results:
EventID P_Num1 PNC1 PN_Name PNC_Num2 PNC2 PN_Name
ABC-I-10942683 1089213 1 Company 11326624 8 Company 2
The EventID will change based on the different events from the companies, as the EventID is based on a particular date the event occurred with the company.
This is just a sample of the 500K+ rows of data I am working with. This will go into a temp table to be joined with the other various pieces of data needed.
I have tried this without success:
SELECT Key,
MAX(Col1) AS Col1,
MAX(Col2) AS Col2,
MAX(Col3) AS Col3
FROM table
GROUP BY Key
select EventID
, min(P_Num) as P_Num1, min(PN_NameCount1) PNC1, min(PN_Name) as PN_Name1
, max(P_Num) as P_Num2, max(PN_NameCount1) PNC2, max(PN_Name) as PN_Name2
from table
group by EventID
This is an answer to the stated question. And you stated in a comment you could extend it.
this is my first time posting so forgive any errors please :)
Source File: Fields
Table 1: ID, Client Number
Table 2: ID, Client Number
Table 1 shows the Customer, and table 2 shows the Bill Payer.Both tables use ID as the transaction ID. So one transaction has 1 record in table 1, and 1 in table 2.
Desired Output:
Table 3: ID, Client Number, Customer/Payer.
I am aware that i can do to append table queries to 1 destination table to achieve but if i can do this with ONE SELECT query that would make the "flow" of my database alot smoother, as i am replacing an old query. Any help would be appreciated and thank you.
SELECT ID, Client_Number, 'Customer' AS Type FROM Table 1
UNION ALL
SELECT ID, Client_Number, 'Payer' AS Type FROM Table 2
SELECT ID, ClientNumber, 'Customer' as Kind FROM Customer
UNION ALL
SELECT ID, ClientNumber, 'Player' as Kind FROM Player
I wanted to know if it was possible to make a select on a table that contains multiple field and join them in 1 result :
Example :
Table :
id
dayOne_City
dayTwo_City
dayThree_City
Result : one column that contains the rows of all the cities (Distinct).
2) Am i better to do a view if i have a lot of query to that specific list ?
3) should i do 3 select with union ?
Thank you
You should be fine with:
select dayOne_City from YourTable
UNION
select dayTwo_City from YourTable
UNION
select dayThree_City from YourTable
However, you should review your design to allow multiple cities per whatever-is-that-your-table stores. That is, create an actual many-to-many relationship by creating an intermediate table between YourTable and Cities.
select concat_ws(',', id, dayOne_city, dayTwo_city, dayThree_city, etc...) as allInOne
Details on the function used here. However, I should ask why you're doing this. By joining the fields together, you're destroy any chance of reliably extracting/separating the data again later. Only do this kind of "bulking" if you never plan on using separate portions of the data elsewhere based on the results of this query.