find subword of given query from table? - mysql

-------------------------------
name | address
-------------------------------
raj kumar | park street
yogin patel | ghari chowk
raju singh | sultan ganj
I searched for park road in table and I could not find it(that's ok).But what query will I use such that I search for parkroad (no space between the words) and at least I receive a details of row containing either park or road or both?
Please give some general Query so that it can be applied for all words whichever I want to search?
Thanks in advance

Maybe something along the lines of:
SELECT name, address FROM table_name
WHERE address LIKE '%park%' or address LIKE '%road%';
You should try this out more to identify what you're exactly looking for.

Related

How to separate one column's data into multiple columns?

Here's my situation : I have a table that has large amounts of records, I need to pull out a number of these records for each name in the database, note that TOP will not work for my use case. My end user wants the report formatted in such a way that each user shows up only once, and up to 3 different dates are shown for the user.
Table format
AutoID
Enum
TNum
Date
Comments
1
25
18
2/2/22
2
25
18
1/2/21
Blah
3
18
18
1/2/21
4
18
18
1/2/20
5
25
17
1/2/22
6
25
17
1/2/20
Now the Enum and TNum fields are fk with other tables, I have created a join that pulls the correct information from the other tables. In the end my query provides this output
RecordID
Training
CompletedDate
FirstName
LastName
Location
2821
MaP
1/1/21
David
Simpson
123 Sesame St.
2822
1/2/22
Fuller
MaP
Dough
GHI
David
123 Sesame St.
2825
1/1/20
Simpson
The two "Blank fields" represent information that is pulled and may or may not be needed in some future report.
So to my question : How do I manage to get a report, with this query's pull to look like this:
Place
LastName
FirstName
Training
FirstCuttoff
Secondcutoff
ThirdCutoff
Comments
123 Sesame St.
David
Simpson
MaP
1/1/20
1/1/21
123 Sesame St.
John
Dough
MaP
1/1/22
I was originally planning on joining my query to itself using where clauses. But when I tried that it just added two extra columns of the same date. In addition it is possible that each record is not identical; locations may be different but since the report needs the most recent location and the name of the trainee. In addition, to add more complexity, there are a number of people in the company with effectively the same name as far as the database is concerned, so rejoining on the name is out. I did pull the Enum in my query, I can join on that if needed.
Is there an easier way to do this, or do I need to sort out a multiple self-joining query?
I have a project I am working on where I am going to have to do this. Some of the suggestions I received were to use a Pivot query. It wouldn't work in my case but it might for yours. Here is a good example
Pivot Columns

List contents of row in a table - but only once

I have a table where one field contains a free to choose text. Some of these texts are identical, some are not. So as an example, this may look like this:
Joe
Jim
Jack
Jack
Jim
Jack
Jane
Now I want to list all the contents of these fields, but compared on a content level so that every data is shown only once. Means the result from my data have to look like this:
Joe
Jim
Jack
Jane
The double-entries Jim and Jack are shown only once although they are contained more often.
My question: is there a SQL-statement which covers this or do I have to filter these data in the result?
Thanks!
Select distinct fieldname from your_table_name
SELECT DISTINCT nameField
FROM YourTable

Merging and converting rows to columns

I have a list of data that happens to have been set up like this:
entryID fieldID Value
100 1 John
100 2 Smith
100 3 USA
101 1 Jane
101 2 Doe
101 3 USA
The way it works is, when a "ticket" is created, it assigns an entryID for all the data in that ticket, but each field also has it's own fieldID. So first name is always a fieldID of 1, last name is 2, etc.
What I need to be able to do is create a view that will look like this:
First Name Last Name Country
John Smith USA
Jane Doe USA
I need to be able to do this in MySQL, and not SQL or excel, as some other similar problems have addressed.
I just found the answer to my own question, so I will post it here for future users.
This website explains how to do exactly what I was asking for: http://stratosprovatopoulos.com/web-development/mysql/pivot-a-table-in-mysql/

Match between two tables where field is substring of the other

Hi (sorry for the poor title),
I have two tables in a MySQL DB, lets call them CarMake and CarModel. Both tables have two fields, ID:int(11) and Description:varchar(100). For example:
CarMake CarModel
ID Description | ID Description
-----------------------------------------------------
123456 Honda | 12345678 Accord
234567 Toyota | 12345665 Civic
369258 Lexus | 23456789 Prius
Where each car model shares the same first 6 digits of the ID of its Make. In this example, both Accord and Civic share the first 6 digits of the ID with Honda, therefore they are Honda models.
Now, what I want to do is select all rows from CarMake that do not have a record in CarModel where the first 6 digits of the ID match. In this example, my query should return the Lexus row from CarMake, as it does not have a matching row in CarModel.
Nothing I have tried so far has really come close to achieving what I want, so I am posting it here.
Any help would be greatly appreciated!
EDIT: Solved with help from zerkms
SELECT * FROM CarMake
LEFT JOIN CarModel
ON CarModel.ID LIKE CONCAT(CarMake.ID, '%')
WHERE CarModel.ID IS NULL;
Follow up questions:
This solution takes a very long time to run, is there any way to improve efficiency?
What would be the best way to delete the records returned by that query? Is there some way I can combine that into the query itself?

saving tree data in database (family tree)

I am trying to store a family tree.
Here is the platform that I am using, Zend framework, Mysql, Ajax
I have searched stackoverflow I came across this post which is very helpful in handling data in terms of objects.
"Family Tree" Data Structure
I'll Illustrate my use case in brief.
User can create family members or friends based on few relations defined in database. I have Model for relations too. User can create family members like Divorced spouse, frineds. Max the Tree can be deep that we are assuming max to kids of the grandchildren but it can expand in width too. Brother/sister & their family.
I am looking an efficient database design for lesser query time. If I have to use the data structures described in above post where I must keep them as they necessary have to be a Model.
For representation I am planning to use Visualization: Organizational Chart from
http://code.google.com/apis/chart/interactive/docs/gallery/orgchart.html#Example
I'll summarize what I need
Database design
Placing of controllers (ajax) & models
The people that the user will create they will not be any other users. just some another data
yeah thats it! I'll post a complete solution on this thread when I'll be completing the project, of course with help of expertise of u guys
Thanks in advance
EDIT I I'll Contribute more to elaborate my situation
I have a user table, a relation table, & last family/family tree table
the Family table must have similar structure to following
ID userid relation id Name
1 34 3 // for son ABC
2 34 4 // for Wife XYZ
3 34 3 // for Mom PQR
4 34 3 // for DAd THE
5 34 3 // for Daughter GHI
6 34 3 // for Brother KLM
The drawback for this approach is generating relations to the other nodes like daughter-in-law, wifes brother & their family.
The ideal way of doing is for a user we can add Parents, siblings, children & for extra relations they must be derived from the family members relation i.e. Brother-in-law must be derived as sister's husband, or wife's brother.
THis is what I can think now. I just need Implementation guidelines.
Hope this helps u guys to provide a better solution.
I guess that from the database point of view it would be best to implement it like
id | name | parent_male | parent_female
Other option would be string prefixing
id | name | prefix
1 | Joe | 0001
2 | Jack | 000100001 //ie. Joes son
3 | Marry| 0001 //ie. Jacks mother
4 | Eve | 0002 // new family tree
5 | Adam | 00020001 // ie. Eves son
6 | Mark | 000200010001 // ie. Adams son
Other (more effective) algorithms like MPTT assume that the data is a tree, which in this case is not (it has circles).
To show it would work - to select Mark's grandparents:
--Mark
SELECT prefix FROM family_tree WHERE id = 6;
-- create substring - trim N 4-character groups from the end where N is N-th parent generation => 2 for grandparent ==> 0002
--grandparents
SELECT * FROM family_tree WHERE prefix = '0002'
-- same for other side of family
-- cousins from one side of family
SELECT * FROM family_tree WHERE prefix LIKE '0002%' AND LENGTH(prefix) = 12