Why DB Browser have an order I can't replicate? - mysql

I have an issue I really don't understand. In fact, I have a database of movies where there's a table for actors,containing two colums (movie id and name).
For example, then I enter the movie id of Django Unchained, the first result is Jamie Fox (the main actor).
But then I enter, this sql query (i would expect to get Jamie Fox, Christoph and Leonardo):
SELECT * FROM LesActeurs WHERE film_id=68718 ORDER BY acteur LIMIT 3
But I get 3 actors by alphabetical order. Do You Know how could I mimic the DB Browser order with command (I'm a beginner)?
Thank you!

Without any other column to order by, you can't get that result, at least not reliably. Without an explicit order by clause, the database is free to return the rows in whatever order it chooses (often it's just the order in which they were inserted).
If you want to get reliably get Jamie, Christophe and Leonardo you could add another column (e.g., "importance"), populate it and then query and explicitly order by it.

Related

Does pymysql's lastrowid function work properly in cases of multiple users inserting into DB?

I have 2 tables in my DB purchase order and lines. Every order can have multiple lines(one line for each part ordered). I am developing an application where an order will first be created. Need to get the ID of this order and then insert lines later on(as the user adds the parts). How can I ensure ther correct value of order ID is fetched?
I cant understand what you exactly want but you should search about ORM its maybe can help you.

MS Access: Delete all records if at least once a condition occurs

Is there a way to, let's say, in a table of phones and their ratings delete all records of a given phone if at least once it received a rating lower than specified?
Company Rating
Samsung 5
Samsung 2
In that example, if I wanted to get rid of phones rated at least once lower than 3, I would have to delete all records that have "Samsung" in it.
You will have to use a subquery for this, for example:
DELETE *
FROM YourTable
WHERE EXISTS (SELECT * FROM YourTable AS tmp WHERE Company = YourTable.Company AND Rating < 3)
Here is a way to work through this problem... (Some would do this in SQL code, as you see in WolfgangK's answer, but I will use the visual query builder, since that is probably going to be easier for you to understand as you are starting out.)
First, here is an example data set. For our problem, we want to delete the records for any phone company with a rating of 3 or less. In this list, we want to delete all the records for LG, even though only one of them has a rating of 3.
If we only wanted to delete the records that have a rating of 3 or less, that would be easy. We would just do it with a delete query like this:
But in your case, you want to delete ALL the records for that company, even if only SOME match the criteria. No problem, just a little more complex. We need to start by identifying the companies that meet the criteria. Let's create a new query called qryBadReviews:
It is very similar to our delete query, but also lists the company name in the results. (We could also group it, if we wanted to only see the company name listed once.)
Now, the next step is to create our delete query that links to the bad reviews query with a matching company name. Simple enough, but then we have a problem:
To fix this, you simply need to change the Unique Records property to Yes in your query, and then it should run for you.
And the resulting data set:
Sorry, LG, it was just an example! I do like your phones. :-)

Alphabetical auto-order in HTML SQL code

I imported the 3 tables in a file to my database. Worked perfectly.
Here's the file code: https://pastebin.com/EVr9qGxe
....
However, I want to change the variable in each table so I went to phpMyAdmin to change the variables in the tables.
They change just fine, however, the options appear in alphabetical, and not the order I entered them in or the country_id
for example if I entered
USA with country_id 1
France with country_id 2
France appears first in the list.
So my question is, what should I change in the code linked above in order to remove the alphabetical auto-order and make the options appear by their ID?
If I understand correctly, your question is similar to this one. There's no way of permanently changing this behavior I'm aware of. You could add
ALTER TABLE `your_table` ORDER BY `column_name`
But the table will 're-order' itself back to where it was on the next CRUD operation performed on that table, and for some databases this query simply won't work.
As you are using phpMyAdmin, there's a simpler solution without coding- just enter your database, choose your table, and click on any column. Clicking on any column will order your table by values from that column. Clicking it again will change ordering from ascending to descending.
Rows in relational database are not really ordered, but you can order them when needed, by performing queries (with use of ORDER BY). For example, if you need to select all rows from table 'users', and order them by 'creation_date', you'd simply use SELECT * FROM users ORDER BY creation_date, but it won't change rows ordering in the table itself- you'd just get ordered result displayed.

MySQL - How to select only results with certain words in them?

I got user profiles on my page. In database, every user has column "mixes", where they put links to their dj mixes. It´s type is varchar.
What Im trying to do: I want to add a "panel" on my homepage, where there´ll be two randomly selected embed dj mixes from the database. Catch is, that some users don´t have filled anything in their "mixes" section, and some of them are using simple links - I want to display only embed mixcloud or soundcloud players, simple link wouldn´t look nice there.
My problem is I don´t know how to handle this by SQL query.
In overall Im looking for query which:
selects 2 random users
they must be users with not empty "mixes" column
in the "mixes" column there must be iframe tag
I don´t know how to explain it better... SQL would be like:
SELECT mixes FROM djs
WHERE mixes IS NOT NULL
AND mixes contains "iframe"
ORDER BY RAND()
LIMIT 2
You must be looking for a LIKE for a match.
SELECT mixes FROM djs
WHERE mixes IS NOT NULL
AND mixes like '%iframe%'
ORDER BY RAND()
LIMIT 2
Refer to:
MySQL: Pattern Matching: LIKE operator

Consistent random ordering in a MySQL query

I have a database of pictures and I want to let visitors browse the pictures. I have one "next" and one "previous" link.
But what I want is to show every visitor anther order of the pictures. How can I do that? If I will use ORDER BY RANDOM() I will show sometimes duplicate images.
Can someone help me please? Thank you!
You can try to use seed in random function:
SELECT something
FROM somewhere
ORDER BY rand(123)
123 is a seed. Random should return the same values.
The problem arises from the fact that each page will run RAND() again and has no way of knowing if the returned pictures have already been returned before. You would have to compose your query in such a way that you can filter out the pictures already presented on the previous pages, so that RAND() will have fewer options to choose from.
An idea would be to randomize the pictures, select the IDs, store the IDs in the session, then SELECT using those IDs. This way, each user will have the pictures randomized, but they will be able to paginate through them without re-randomizing them on each page.
So, something like:
SELECT id FROM pictures ORDER BY RAND() LIMIT x if you don't have the IDs in the session already
Store the IDs in the session
SELECT ... FROM pictures WHERE id IN (IDs from session) LIMIT x
Another idea is to store in session the IDs that the user already saw and filter them out. For example:
SELECT ... FROM pictures ORDER BY RAND() LIMIT x if the session doesn't contain any ID
Append the IDs from the current query to the session
SELECT ... FROM pictures WHERE id NOT IN (IDs from session) ORDER BY RAND() LIMIT x
Another way seems to be to use a seed, as izi points out. I have to say I didn't know about the seed, but it seems to return the exact same results for the exact same value of the seed. So, run your usual query and use RAND(seed) instead of RAND(), where "seed" is a unique string or number. You can use the session ID as a seed, because it's guaranteed to be unique for each visitor.
You can seed the random function as suggested by izi, or keep track of visited images vs non-visited images as suggested by rdineiu.
I'd like to stress that neither option will perform well, however. Either will lead you to sorting your entire table (or the part of it of interest) using an arbitrary criteria and extracting the top n rows, possibly with an offset. It'll be dreadfully slow.
Thus, consider for a moment how important it is that every visitor should get a different image order. Probably, it'll be not that important, as long as things look random. Assuming this is the case, consider this alternative...
Add an extra float field to your table, call it sort_ord. Add an index on it. On every insert or update, assign it a random value. The point here is to end up with a seemingly random order (from the visitor's standpoint) without compromising performance.
Such a setup will allow you to grab the top n rows and paginate your images using an index, rather than by sorting your entire table.
At your option, have a cron job periodically set a new value:
update yourtable
set sort_ord = rand();
Also at your option, create several such fields and assign one to visitors when they visit your site (cookie or session).
This will solve:
SELECT DISTINCT RAND() as rnd, [rest of your query] ORDER BY rnd;
Use RAND(SEED). From the docs: "If a constant integer argument N is specified, it is used as the seed value." (http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_rand).
In the example above the result order is always the same. You simply change the seed (351) and you get a new random order.
SELECT * FROM your_table ORDER BY RAND(351);
You can to change the seed every time the user hits the first page.
Without seeing the SQL I'd guess you could try SELECT DISTINCT...