SQL join table 2 find - mysql

Table reserve
sib boot_id day
22 101 10/10/1996
22 103 11/12/1996
Table Sailors
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0
28 yuppy 9 45.0
44 gruppy 8 55.5
The result I want is find name who borrow all boot ?

You should definetely try a basic tutorial, there is a good one at Khan Academy: https://www.khanacademy.org/computing/computer-programming/sql.
It is a simple join. Since one person has both items, I've also included a distinct not to repeat the name.
Your answer is :
select distinct ts.sname
from Table_Sailors as ts
inner join Table_Reserve as tr on ts.sid = tr.sib
Just change the table names, Table_Sailors and Table_Reserve, to whatever table name you have in your database.

Related

SQL QUERY to show records of names that are recorded several times and are unique towards each other

For example, let us consider this table:
In this image consists of rows of 8 where names like Mike,Glenn,Daryl,Shane and Patricia is included with their respective ages
Id
Name
Age
1
Mike
25
2
Glenn
19
3
Glenn
19
4
Daryl
56
5
Shane
30
6
Shane
30
7
Patricia
16
Now I want to insert the type of query that will show the names without repetitions like This, not like This
EDIT: I entered the data from first picture. The request is to list the names without duplicates, as shown in the second and third picture but I will not convert them to text.
DISTINCT specifies removal of duplicate rows from the result set.
SELECT DISTINCT Name
FROM tablename
see: use DISTINCT in SELECT
You can use GROUP BY to achieve it.
SELECT * FROM your_table
GROUP BY your_table.name
ORDER BY id
With the data you gave, the result from this query will be:
id
name
age
1
Mike
25
2
Glenn
19
4
Deryl
56
5
Shane
30
7
Patricia
16

MySQL Use Distinct or Group By in XREF Table Query

I'm building a php configurator with a series of relationships which I'm controlling with MySQL XREF tables.
There is one XREF table which has multiple dependencies as below:
Table: cto_body_deck_rear_chassis_xref
body_id
deck_type_id
rear_id
chassis_id
22
20
23
13
23
20
18
17
23
20
21
17
23
20
24
17
24
20
18
17
25
21
22
14
Each complete combination is unique although there are similarities between columns; however, I'm getting a duplication problem when selecting from a deck type table, relative to a body id variable passed in the URL.
Table: cto_deck_type
deck_type_id
deck_type_content
20
Single Deck
21
3/4 Length Fixed 2nd Deck
22
Full Length Fixed 2nd Deck
If I use the following MySQL statement:
SELECT d.deck_type_id, d.deck_type_content
FROM cto_deck_type d
LEFT JOIN cto_body_deck_rear_chassis_xref xref
ON xref.deck_type_id = d.deck_type_id
WHERE xref.body_id = 23
I get 3 results, even though each result is identical because the body_id and deck_type_id match 3 times (20).
If the results are identical, I want to group them together or select distinct but I'm not sure what the statement should look like?
Any assistance would be appreciated.
SELECT d.deck_type_id, d.deck_type_content
FROM cto_deck_type d
LEFT JOIN cto_body_deck_rear_chassis_xref xref
ON xref.deck_type_id = d.deck_type_id
WHERE xref.body_id = 23
;; and add the line
GROUP BY d.deck_type_id, d.deck_type_content

join same type table data in a table in sql

In the past I entried my data month wise due to lack of my knowledge in the month name table. But now I easily filter them, in my database, there are 6 months (Oct - Feb) table are there with same row name month-wise data can I put all the data in a table, for manually put them in a file little bit difficult for me because of id,
so Please suggest to me to make it easily
for example, the October 2018 table is this
id user_name date nota veet tree location
1 milon 10/10/2018 43 12 111 bandel
2 kadir 11/10/2018 12 34 76 katwa
3 javed 22/10/2018 33 56 92 sirampur
4 milon 29/10/2018 55 21 78 salar
november 2018 table is
id user_name date nota veet tree location
1 milon 10/11/2018 13 12 71 Rampurhat
2 kadir 11/11/2018 12 24 76 katwa
3 javed 12/11/2018 53 30 62 kandi
4 milon 24/11/2018 55 27 58 salar
now I want SQL table like this
id user_name date nota veet tree location
1 milon 10/10/2018 43 12 111 bandel
2 kadir 11/10/2018 12 34 76 katwa
3 javed 22/10/2018 33 56 92 sirampur
4 milon 29/10/2018 55 21 78 salar
5 milon 10/11/2018 13 12 71 Rampurhat
6 kadir 11/11/2018 12 24 76 katwa
7 javed 12/11/2018 53 30 62 kandi
8 milon 24/11/2018 55 27 58 salar
You can create a new table (for example user_locations). Then you can copy data from first table and then copy data from second table. If you have ID column with auto-increment, and if you do not specify the ID column in select, than the ID will be assigned automatically and there will not be collision of IDs. Example of SQL for selecting from one table and inserting to another table:
INSERT INTO user_locations (user_name, date, nota, veet, tree, location)
SELECT user_name, date, nota, veet, tree, location FROM user_locations_november_2018
... however I am not sure that I understand you correctly. What exactly are you trying to do? Move data from all tables to one table (this is the one I answer). Or just select data from all the tables (in which case the UNION from other answer is correct). Or to select all data and put them into a text file?
select * from table1 Union Select * from Table2
use The SQL UNION Operator

Missing values on count in mysql

I'm just stuck with this issue atm and I'm not 100% sure how to deal with it.
I have a table where I'm aggregating data on week
select week(create_date),count(*)
from user
where create_date > '2015-02-01'
and id_customer between 9 and 17
group by week(create_date);
the results that I'm getting have missing values in the count, as shown below
5 334
6 376
7 394
8 405
9 504
10 569
11 709
12 679
13 802
14 936
15 1081
16 559
21 1
24 9
25 22
26 1
32 3
34 1
35 1
For example here from 16 to 21 there a obviously 4 values missing I would like these values to be included and count to be 0. I want this because I want the weeks to be matching with other metrics as we are outputting them in an excel file for internal analysis.
Any help would be greatly appreciated.
The problem is that an sql query cannot really produce data that is not there at all.
You have 3 options:
If you have data for each week in your entire table for the period you are querying, then you can use a self join to get the missing weeks:
select week(t1.create_date), count(t2.id_customer)
from customer t1
left join customer t2 on t1.id_customer=t2.id_customer and t1.create_date=t2.create_date and t2.id_customer between 9 and 17
where t1.create_date > '2015-02-01'
group by week(t1.create_date)
If you have missing weeks from the customer table as whole, then create a helper table that contain week numbers from 1 or 0 (depending on mysql config) to 53 and do a left join to this helper table.
Use a stored procedure that loops through the results of your original query and inserts the missing data in the resultset using a temporary table and then returns the extended dataset as result.
The problem is that there is no data matching your criteria for the missing weeks. A solution will be to join from a table that has all week numbers. For example if you create a table weeknumbers with one field weeknumber containing all the numbers from 0 to 53 you can use something like this
select weeknumber,count(user.*)
from weeknumbers left join user on (weeknumbers.weeknumber=week(user.create_date)
and user.create_date > '2015-02-01'
and user.id_customer between 9 and 17)
group by weeknumber;
Additionaly you might want to limit the week numbers you do not want to see.
The other way is to do it in the application.

Using group_concat how to iterate each value as a column

I am using MySQL.
I want to be able to always return 1 row with 5 columns on my query. I have a student table and a committee table as follows:
Student Info Table
Student_Number Committee_Id
00000001 5
Committee Table
Committee Id Prof_Id
5 23
5 55
5 6
5 10
Using this statement:
select committee_id, group_concat(prof_id) as profs from committee_table
I get:
profs committee_id
23,55,6,10 5
I want to return:
prof1 prof2 prof3 prof4 prof5
23 55 6 10 null
Is there any way to do this simply?????
how about you try creating your committee table like this:
create table committee(committee_id datatype,prof1 datatype,prof2 datatype,prof3 datatype,prof4 datatype,prof5 datatype);
since the number of professors are static.