I want to perform a query whereby I want to check whether on the columns A has either certain values. A could have only X , X and Y or a combination of X Y and Z.
To give a better understanding. I am checking a book's author within a table itself. The table has the BOOK_ID , BOOK_TITLE , AUTHOR_NAME, AUTHOR_ORDER.
So a book might have 1,2 or 3 authors, listed in order written inside the AUTHOR_ORDER row. I am trying very hard to reach an output where if a book has 3 authors, it will display accordingly from the first author to the third author. I am now stuck in the part where I need to compare the value and present it in the output.
Any idea how to achieve this in MYSQL output?
Sample :
The output result is more or less like this:
If the title has au_ord of 1,2 and 3, there shall be a new column with all the authors name listed in ascending.
So for example, for title BU1032, the Author row will be Bennet, Green
I think GROUP_CONCAT is what you are after:
SELECT Title_ID,
Title,
GROUP_CONCAT(au_LName ORDER BY au_Ord) AS Authors
FROM Books
GROUP BY Title_ID, Title;
SQL FIDDLE
Related
I have a database as follows
The first column is the name.
The second column bears the number of the son in the family.
The third column is the family Code.
-Explanatory information:
-The first column in the database. Contains the name of the father and children and they are not arranged
-The second column in the database. It contains the number of fathers and children in the database where the parents take nothing and the children take numbers.
-The third column in the database. It contains a code that is specific to each family and does not subscribe to any other family.
What is required when searching in query as follows:
The query is made for a specific name, where the search is for displaying all fields containing the same name if it is for a father or children.
Provided that the final form of the offer is to collect parents only and arrange them in ascending order.
The data in the images is for illustration and the actual column names of the query
If you search for a name, let it be Ali, the result will be as follows 2 image:
2- The final result With the order of the parents in ascending name order.
MY TRY NOT WORK
SELECT PARENT_NAME, PARENT_NUMBER,PARENT_CODE
FROM PARENT_TB
HAVING (((PARENT_NUMBER) Is Null));
Try using a subquery:
SELECT
IIF(PARENT_NUMBER Is Null,
PARENT_NAME,
(Select T.PARENT_NAME
From PARENT_TB As T
Where T.PARENT_CODE = PARENT_TB.PARENT_CODE And T.PARENT_NUMBER Is Null)) As ParentName,
PARENT_CODE
FROM
PARENT_TB
WHERE
PARENT_NUMBER = "Ali"
I'm fairly new to SQL and have a question regarding a query.
I have a database with various pictures attached to a product. All these pictures have a prediction. The structure is like this:
product_ id picture_id prediction
1------------pic1.jpg----------type a
1------------pic2.jpg----------type b
1------------pic3.jpg----------type b
2------------pic4.jpg----------type a
2------------pic5.jpg----------type a
2------------pic6.jpg----------type a
3------------pic7.jpg----------type c
...
... so on.
Each pictures is predicted individually and because of that some of the products have contradictory predictions (meaning that on the same products some pictures are predicted type a while others are predcited type b).
I want to filter out all of these products with a query. In other words: I need all product_ids where the predictions for the pictures linked to it are not all the same. In our example I want it only to show me product 1.
I tried some stuff with GROUP BY, but have not yet gotten anywhere near the result that I want.
Thanks for helping,
Cheers
Use COUNT(DISTINCT prediction) to get the number of different predictions. It will be more than 1 for the products with different predictions.
SELECT product_id
FROM yourTable
GROUP BY product_id
HAVING COUNT(DISTINCT prediction) > 1
Note: You can find the previous question and its answer here. A deep testing on it proved the previous answer is incorrect: Writing a Complex MySQL Query
I have 3 tables.
Table Words_Learned contains all the words known by a user, and the order in which the words were learned. It has 3 columns 1) word ID and 2)user id and 3) order in which the word was learned.
Table Article contains the articles. It has 3 columns 1) article ID, 2) unique word count and 3) article contents.
Table Words contains a list of all unique words contained in each article. It has 2 columns 1) word ID and 2) article ID
The database diagram is as below/
Now, using this database and using "only" mysql, I need to do the below work.
Given a user ID, it should get a list of all words known by this user, sorted in the revese order from which they were learned. In other words, the most recently learned words will be at the top of the list.
Let’s say that a query on a user ID shows that they’ve memorized the following 3 words, and we track the order in which they’ve learned the words.
Octopus - 3
Dog - 2
Spoon - 1
First we get a list of all articles containing the word Octopus, and then do the calculation using table Words on just those articles. Calculation means if that article contains more than 10 words that do not appear in the user’s vocabulary list (pulled from table words_learned), then it is excluded from the listing.
Then, we do a query for all records that contain dog, but DO NOT contain “octopus”
Then, we do a query for all records that contain spoon, but DO NOT contain the words Octopus or Dog
And you keep doing this repetitive process until we’ve found 100 records that meet this criteria.
To achieve this process, I did the below (Please visit the SQLFiddle link to see the table structures, test data and my query)
http://sqlfiddle.com/#!2/48dae/1
In my query, you can see the generated results and they are invalid. But on a "Proper Query", the result should be,
Level 1
Level 1
Level 1
Level 2
Level 2
Level 2
Level 3
Level 3
Here is a phudocode for better understanding.
Do while articles found < 100
{
for each ($X as known words, in order that those words were learned)
{
Select all articles that contain the word $X, where the 1) article has not been included in any previous loops, and 2)where the count of "unknown" words is less than 10.
Keep these articles in order.
}
}
select * from (
select a.idArticle, a.content, max(`order`) max_order
from words_learned wl
join words w on w.idwords = wl.idwords
join article a on a.idArticle = w.idArticle
where wl.userId = 4
group by a.idArticle
) a
left join (
select count(*) unknown_count, w2.idArticle from words w2
left join words_learned wl2 on wl2.idwords = w2.idwords
and wl2.userId = 4
where wl2.idwords is null
group by w2.idArticle
) unknown_counts on unknown_counts.idArticle = a.idArticle
where unknown_count is null or unknown_count < 10
order by max_order desc
limit 100
http://sqlfiddle.com/#!2/6944b/9
The first derived table selects unique articles a given user knows one or more words from as well as the maximum order value of those words. The maximum order value is used to sort the final results so that articles containing high order words appear first.
The second derived table counts the number of words a given user doesn't know for each article. This table is used to exclude any articles that contain 10 or more words the user doesn't know.
this is a revised question from previous one. I decided to open a new question since the scope has changed.
This is what I want to achieve.
List the last name of the author(s) of the book in an "Author" column, the last name of the author to be listed first, followed (after a comma) by that of the author to be listed second if any; and if there is a third author, put it after the second author's name. The order of the author of a book is listed in the au_ord column (1=First Author, 2=Second Author, 3=Third Author).
Any idea how to achieve this in MYSQL output?
Those are the source table. The desired output is something like this:
This should give your first sample output:
SELECT title_id, au_ord, au_lname
FROM title_authors
LEFT JOIN authors
ON authors.au_id = title_authors.au_id
WHERE title_id = 'TC7777'
ORDER BY au_ord;
and this should give the second:
SELECT title_id, GROUP_CONCAT(au_lname ORDER BY au_ord SEPARATOR ', ')
FROM title_authors
LEFT JOIN authors
ON authors.au_id = title_authors.au_id
GROUP BY title_id
HAVING title_id = 'TC7777';
All you need is a GROUP_CONCAT function.
SELECT title_id, GROUP_CONCAT(au_lname ORDER BY au_ord)
FROM table
GROUP BY title_id
The GROUP BY clause groups all individual (distinct) titles, and the GROUP_CONCAT on it concatenates all the authors of it. This would list all the titles and their corresponding authors. If au_lname is from another table than from title_id table, then you will have to use appropriate joins. I am not sure what are the table names from your question.
I don't think this is a duplicate posting because I've looked around and this seems a bit more specific than whats already been asked (but I could be wrong).
I have 4 tables and one of them is just a lookup table
SELECT exercises.id as exid, name, sets, reps, type, movement, categories.id
FROM exercises
INNER JOIN exercisecategory ON exercises.id = exerciseid
INNER JOIN categories ON categoryid = categories.id
INNER JOIN workoutcategory ON workoutid = workoutcategory.id
WHERE (workoutcategory.id = '$workouttypeid')
AND rand_id > UNIX_TIMESTAMP()
ORDER BY rand_id ASC LIMIT 6;
exercises table contains a list of exercise names, sets, reps, and an id
categories table contains an id, musclegroup, and type of movement
workoutcategory table contains an id, and a more specific motion (ie: upper body push, or upper body pull)
exercisecategory table is the lookup table that contains (and matches the id's) for exerciseid, categoryid, and workoutid
I've also added a column to the exercises table that generates a random number upon entering the row in the database. This number is then updated only for the specified category when it is called, and then sorted and displays the ascending order of the top 6 listings. This generates a nice random entry for me. (Found that solution elsewhere here on SO).
This works fine for generating 6 random exercises from a specific top level category. But I'd like to drill down further. Here's an example...
select all rows inside categoryid 4
then still within the category 4 results, find all that have movementid 2, and then find one entry with a typeid 1, then another for typeid 2, etc
TLDR; Basically there's a few levels of categories and I'm looking to select a few from here and a few from there and they're all within this top level. I'm thinking this could all be executed within more than one query but im not sure how... in the end I'm looking to end with one array of the randomized entries.
Sorry for the long read, its the best explanation I've got.
Just realized I never came back to this posting...
I ended up using several mysql queries within a switch based on what is needed during the request. Worked out perfectly.