Retrieve data from two table using id [duplicate] - mysql

This question already has answers here:
SQL query return data from multiple tables
(6 answers)
Closed 5 years ago.
Table1
Id, name, addresses
101,raja ,chennai
Table2
Id,group,name
101,a,siva
102,b,selva
I want retrieve data from two tables like
Table2.group=a and two table Id must equal then take address from table1 display
Id, name, Address, group

What have you tried so far? Have you taken a look at some of the resources here?
Since this is the type of pretty straightforward SQL query you're likely to do many times over, it's worth you reading the tutorials and learning it yourself. I'll give you a few pointers to begin:
Your two tables are connected by the Id field (most likely, this is the primary key for one table and the foreign key for another). You'll need to JOIN these two tables using that field.
You'll need to specify a WHERE clause to filter for your condition that Table2.group = a.
The fields you want to display can all be specified using SELECT. If you are selecting data FROM Table 1 and joining to Table 2, you won't need to specify the table name before the field name.

Related

Link a field either to ID on one table or to ID on another

In MS-Access, I've got a table where its key input is either a primary key of one type (of companies) or the primary key of another type (of companies). There's virtually no overlap in fields between these two types (hence they're separate tables).
How does one structure this as links etc just relate to one table?
Thank you
Use a union query. The official guide is here:
Use a union query to combine multiple queries into a single result

Multiple rows inside each table row (but only on certain columns)

I'm not quite sure how to word this so I've not managed to find an answer for it!
I want to create a table, where in certain columns there is multiple rows. As shown in the picture
How do I structure this?
An example of what I'm trying to achieve is imagine a table that listed all users in an application. Each row is a user, but I want to also have a sub row for each of the photos that a user may have.
You cannot have multi-valued columns. As they do not satisfy first normal form (For detailed information about 1st Normal form: https://en.wikipedia.org/wiki/First_normal_form)
Let your table have the following columns:
(A,B,C,D)
As per the picture provided by you, I am making the following assumptions:
1) A is the primary key
2) C and D are both are multi-valued and the rows have values like (A1,B1,C1,D1),(A1,B1,C2,D2). That is for single A value we have multiple pairs of C and D.
Do comment if any of the assumption is wrong.
What you can do is make two tables.
TABLE1 (A,B)
TABLE2 (A,C,D)
Where A is the primary key in TABLE1 and foreign key in TABLE2.
As asked for a snippet,you can have tables like these:
Tables

MySQL merge multiple tables that share similar columns

I have 5 tables, each with 10+ columns. They all share some similar columns but each table has columns unique to themselves. The tables are not related to each other, meaning that one record in one table won't be able to exist in another table. Therefore, when I try to concatenate them using UNION ALL, I get a very complicated MySQL statement since the tables have so many columns. For example, I have 2 tables:
Table1: ID, Name, Parts, Comments, End_Date
Table2: ID, Name, Machines, End_Date
If I were to combine these two tables, I would use the MySQL statement
SELECT
ID,
Name,
Parts,
'' as Machines,
Comments,
End_Date
FROM Table1
UNION ALL
SELECT
ID,
Name,
'' as Parts,
Machines,
'' as Comments,
End_Date
FROM Table2
As you can see, the statement gets much larger the more columns and more tables there are. Is there an elegant way to concatenate these tables in a more concise statement? Thanks!
For combining tables, you can use the FOREIGN KEY method from SQL that will work in MySQL. To do this, outside of the creation of your table:
ALTER TABLE Table1
ADD FOREIGN KEY (End_Date) REFERENCES Table2(End_Date);
To do this inside the creation of your table, you would just code it in after your primary key and use the bit that includes FOREIGN KEY and everything after except the ' ; '. This method will only work with one column from each table though.
For extra help that you could research if you so choose to, this website has the basic methods of all SQL languages and is extremely helpful:
SQL Tutorials

Syncronizing entries in a table according to a user array in MySQL

I have the following situation. The entries in a table come from a list of users for a particular id. That way id 7 has 3 rows for, say, 2, 6, 7 (these three are unique ids for a user data table). To clarify the table looks like this.
ID USERID KEYID
Where KeyID is auto_inc and is the table's primary key.
These entries come from a multiple select field. So a user might want to delete user 2 and add user 8. So the function that needs to update the table gets the array 8,6,7 for id 7. The quick way I found to do the syncronization is simply to delete every entry in the table for ID 7 and add 3 new entries of the from (ID,USERID) (7,8) (7,6) (7,7).
However I don't know if this is how it's supposed to be. Is there a better way? Also this methods drives the keyid up really fast (for every modification in the table, basically). Is that a problem? I'm an newbie with these things, so please be patient.
Well, you can delete rows selectively like this:
DELETE FROM my_table WHERE user1 = 7 AND user2 NOT IN (8,6,7)
where (8,6,7) is list of pairs (7,6),(7,8),(7,7) , that should be preserved, kept, not deleted, and it will delete all other pairs (7,?)
Your next question may be "Then how to add pairs while not making duplicities?"
You will first need to teach your table what duplicity is. By adding unique key on pair of fields (user1,user2). That would forbid duplicities. Now, when inserting new rows that may be already there, use "INSERT IGNORE" to ignore such exception and simply, continue on.

mysql - It is ok to have n amount of columns?

I know it's possible to have n amount of columns, but is it proper mysql "coding standard"?
Here is what I'm doing:
I am a table student which includes all the students info including testScores:
student
-------
studId
name
age
gender
testId
Instead of putting each individual test answer within the student table, I made a separate table called testAnswers that will hold each students test results:
testAnswers
-----------
testId
ques1
ques2
.
.
.
quesN
Each entry in the testAnswers table corresponds to a specific student in the table student.
Of course, there will be an admin that will be able to add questions and remove questions as each year the test questions may change. So, if the admin were to remove an answer, than that means one of the columns would be removed.
Just to reiterate myself, I know this is possible to edit and remove columns in a table in mysql, but is good "coding standard"?
The answer is a simple and clear: No. That's just not how you should do it except for very few corner cases.
The usual way to approach this is to normalize your database. Normalization follows a standard procedure that (among other things) avoids having a table with columns names ques1, ques2, ques3 ....
This process will lead you to a database with three tables:
students - id, name, and other stuff that applies to one student each
questions - id and question text for each question
answers - this is a N:M relation between students: student_id, question_id, answer_value
Use two tables!
What you are describing is a one to many relationship as there can be one student to many test scores. You would need to have some id as a foreign key to the student_id and put this id in the testAnswers table. You can then set constraints, which tell the database how to handle removal of data.
As one commenter has mentioned, using one table would result in breaking 1nf or first normal form which basically says that you cannot have multiple values for a single column given a particular record - You can't have multiple test scores for the same user in a given table, instead break the data up into two tables.
...of course 2 tables, also could use 3, just remember to insert a studId column also in the testAnswers table (with REFERENCE to the student table) and an INNER JOIN testAnswers ON student.studId=testAnswers.studId at the SELECT query (to read the data).