select by order - mysql

Thare are two tables: image and car. They are linked by one-to-many unidirectional relationship.
I want to make something like gallery: simultaneously load only one photo of a car, and other photos are available via links [1], [2], [3], ...
Question: but how can I pull photo by it order. It means I don't know image's ids (they may be 10, 23, ....) and/ for example, I don't know which image I should pull via [3] link.
P.S. I pull images via command: SELECT * FROM image WHERE car_id_fk=?

use limit - keep the second number at 1, and for the first number put the selected link - 1
select * from image where card_id_fk=? limit 0,1

Related

How do I get all strings that do not contain another string in MySQL?

I have a table called "Domains" with field "Name" (unique, always lowercase) which contains a list of domains and subdomains on my server like:
blah.example.com
www.example.com
www.blah.example.com
example.com
example.nl
example.org
Looking at this list, names 1, 2 and 3 are subdomains of item 4. And I'm looking to just find all domains in this table without these subdomains. Or, to be more precise, any name that does not have part of it in the name from another record. Thus only item 4, 5 and 6.
If record 4 was missing then this query would also have item 1 and 2 as result, but not item 3. After all, item 3 has item 1 as part of it.
Just trying to find the query that can provide me this result... Something with select d.name from domains where d.name not in... Well, there my mind goes blank.
Why?
This list of domains is generated by my web server which registers every new domain that gets requested on it. I'm working on a reporting page where I would display the top domain names to see if there are any weird domains in it. For some reason, I sometimes see unknown domain names in these requests and this might give some additional insight in it all.
I am going to change my code so it will include references to parent domains in the same table in the future but for now I'll have to deal with this and a simple SQL solution.
Use a self-join that matches on suffixes using LIKE
SELECT d1.name
FROM domains AS d1
LEFT JOIN domains AS d2 ON d1.name LIKE CONCAT('%.', d2.name)
WHERE d2.name IS NULL
DEMO

MYSQL database - How to work with an "order" column that stores the order items should be displayed - grouped by another column

This could by an x/y problem, so if there's a better approach altogether, I'd love to hear it. The summary of the problem starts at the last code block, so skip to that if you want and come back to the details if needed.
I am building a content manager (if nothing else, for the experience). To get started understanding what data I need, I made a "pages" table with this structure:
id (page id) | path (where it is found) | title | content | (etc.. some other stuff)
So, it is the content area that is trouble. Let me explain the end result: I need a content map that has an object of positions, each which are arrays of content that belongs in that position. Here's a sample:
{ header: [5], main: [4,1], footer: [2,8,9] }
Those id's will then go to a content table, pull each item (like id 5) and replace the id with the actual content/settings for that item. That isn't really as relevant for now.
I can't just store the json right to the db in the content field of "pages" because if I were to delete content item "5", it would still be in the json for that page. I need to be able to delete content item 5, and it automatically be removed from wherever it is used.
That lead me to this:
I create another table that tracks where content items are used and the order. Here's the structure for that table (content_locations):
pageId (what page this content is on) | contentId (which content) | position | order
So, I think that gets me on the right track on being able to delete things... if I delete a page, I believe I can set it up to delete the rows it has in content_locations and also set it up so that removing a content item will remove the content_locations rows for that item. I honestly haven't tried that yet, but I'm pretty sure that's possible. If not, I'm really lost :)
My main issue seems to be the ordering. Consider this set of data:
pageId, contentId, position, order
2, 6, header, 0
2, 1, header, 1
2, 4, header, 2
How could I insert a first item (can't insert before 0) or what if I wanted to insert in between one of those (1 1/2) or what if I deleted item 1? I run into a big problem with reordering. Is this a problem with my idea of how to structure the data, or is there a good solution for dealing with an ordering column such as that?
Other issues notwithstanding, I'll just comment on reordering...
If you need to make space to insert a new row, you can easily move the other rows out of the way by:
UPDATE your_table
SET order = order + 1
WHERE pageId = ... AND order > 0
(Replace 0 with the actual position at which you want to insert the new row.)
You can do the opposite after delete, or you can just leave a hole - these holes can be easily "collapsed" at the presentation level.
Unless you have a large number of rows per page, this should be reasonably quick. If not, consider leaving holes in advance, and moving elements only if the hole is completely filled.
BTW, to switch two rows, you can do something like this:
UPDATE your_table
SET order =
CASE order
WHEN 2 THEN 3
WHEN 3 THEN 2
END
WHERE pageId = ... AND order IN (2, 3)
(Replace 2 and 3 with actual positions.)
I think the correct way to do this is to have an after column that references the pageId to say that this is after that. Although that's going to make for some complex SQL. Alternatively just start the order off as 0, 1000, 2000, which gives space to insert things.
One way to do the insert is to multiply every order value by two and add one -- this is a fairly trivial query. Then you have space to insert, so your table becomes:
pageId, contentId, position, order
2, 6, header, 1
2, 1, header, 3
2, 4, header, 5
and then you just insert the order as (order * 2) and it's guaranteed to have a space.
Periodically you'll have to collapse the numbering in order to stop overflowing the values -- but if you check if there's a space beforehand then this should be rare. (You can use ROW_NUMBER to do the renumbering).
I wouldn't embed JSON in the database at all if you can avoid it, parse it and add it to separate tables to make your life easier later.

Every field can have multiple values

I am asked to allow users to input multiple values in EVERY field. So the option is limitless.
For example. Columns are:
CompanyID-
Company name
Website
Key_Markets
M&A_History
Highlights
Region
Comments
A scenario is a company can have multiple websites,key markets, region, etch. How would I do this professionally? I am thinking of putting every column a seperate table.
Basically there are three ways to realize this.
1) Write multiple fields into one column seperately. This would be a very bad design and you would have to handle the splitting in your application - Do not do that ;-)
2) Use one table with multiple groups to store the data. This would make sense for parameters but not really if you have different values for each customer. For example:
CompanyID
GroupID
Position
Value
Example:
108001, 'homepage', 1, 'www.mypage.com';
108001, 'homepage', 2, 'www.mysecondpage.com';
108001, 'homepage', 3, 'www.anotherpage.com';
108001, 'markets', 1, 'erp';
108001, 'markets', 2, 'software';
108001, 'region', 1, 'germany';
108001, 'region', 2, 'austria';
108001, 'region', 3, 'poland';
3) Use seperate tables for each 1:n relation! This would be the best solution for your needs I guess. This would have the advantage that you can easily extend your schema and store more data in it. For example if you decide to store the amount of users for each region or key markets etc.
Another point: Use n:m relations to avoid double content in your database! For example should the key-markets and regions be stored in a completely seperated table and you store the IDs of the customer and the key-market in a crosstab. So you do not need to store the key-markets as a string for each customer!
You would need a database structure like:
table_master_companies
- record_id
- company_name
table_websites
- record_id
- company_id
- website_address
table_key_markets
- record_id
- company_id
- key_market
etc. You would then need to use joins to concat all the information into a single recordset.

Fusion Tables filter conditions OR

According to https://developers.google.com/fusiontables/docs/developers_reference OR operations are not allowed as part of filter conditions. So I'm trying to come up with a creative way to solve the following:
I have a fusion table backed google map with hundreds of places and want to filter it to only places that have 'tags' or a 'title' containing a search parameter.
Ideally I could just use the following as my filter condition:
tags CONTAINS IGNORING CASE 'searchterm' OR title CONTAINS IGNORING CASE 'searchterm'
But the fusion table API simply doesn't allow it. So what to do? Make 2 separate queries, then filter out the duplicates? That means I can't use the nice FusionTablesLayer pre-rendered tile functionality.
What would you do?
A possible answer is to pre-render the data within the table. Essentially add another column which is an aggregate of tags and title. Then I only need to query the one 'tags_or_titles' column. Of course this means more data munging beforehand when I export the data into the fusion table and doesn't feel so nice and clean...
How about adding a column to your table called "Show_In_Results",
then running two separate queries to update that column for each row of data based on whether the search term is found in the specific column or not.
UPDATE 'table_id'
SET Show_In_Results = 1
UPDATE 'table_id'
SET Show_In_Results = 1
WHERE tags CONTAINS IGNORING CASE 'searchterm'
UPDATE 'table_id'
SET Show_In_Results = 1
WHERE title CONTAINS IGNORING CASE 'searchterm' and Show_In_Results <> 1
Then when you render your map layer:
SELECT 'columns' FROM 'table_id' WHERE Show_In_Results = 1

Relational Database simple search string

Hey guys,
This is a follow-up to a question that I asked earlier. It is my first time using a relational database and I need help with a quick search string to bring up desired results.
Background information: I'm making a database for my photo portfolio and want to be able to retrieve image links/data via their categories. Each image can be listed in multiple categories.
My database is set-up as follows :
TABLE tbl_images (image_id, image_title, image_location, image_descrip,image_url)
TABLE tbl_categories (category_id,category_name,category_descrip)
TABLE tbl_image_categories (image_id,category_id)
Where one of my images (image_id=1) has two categories (Desert [category_id=1] and Winter [category_id=2]). Which I defined in tbl_image_categories as 1,1 and 1,2.
I also have a few other images that I defined as Desert images [category_id=1].
How would I go about getting which images should be loaded based on the Desert Category?
I tried:
SELECT tbl_images.image_url
FROM tbl_images,
tbl_image_categories,
tbl_categories
WHERE tbl_categories.category_id = 1
Try this:
SELECT DISTINCT tbl_images.image_url
FROM tbl_images,
tbl_image_categories,
tbl_categories
WHERE chad_categories.category_id = 1 //category_id=1 for Desert
AND chad_images.image_id = chad_image_categories.image_id
AND chad_image_categories.category_id = chad_categories.category_id