SELECT INTO multiple #variables MySQL - mysql

I wish to store the result of a SELECT statment into multiple variables.
I know that the result of this query will always return 6 ints from 6 different rows.
I have tried using the following code :
SELECT id INTO #photo1, #photo2, #photo3, #photo4, #photo5, #photo6
FROM album WHERE uploaded = #time AND scene_id = NEW.id;
album is a table.
uploaded is a field in album.
scene_id is a field in album.
id is the PRIMARY_KEY of album.
I have read that the number of variables must equal the number of fields. Which is obviously not the case in the above statement.
With this in mind, how would I overcome this problem?
This code is being used within a MySQL Trigger.
EDIT : Relevant Table schema as per request :
person -- name of table
id | approved -- id is PK
album -- name of table
id | uploaded | scene_id -- id is PK
Trigger is fired on change of approved from 0 to 1

You can join with the same table and ensure that each join will provide a new id, something like (eg. for two ids, but you will get the point):
SELECT a1.id, a2.id INTO #photo1, #photo2
FROM album a1
inner join album a2 on a2.scene=a1.scene and a2.upload=a1.upload and a2.id>a1.id
WHERE a1.uploaded = #time AND a1.scene_id = NEW.id;
See SqlFiddle for a complete sql and test case.

Related

MYSQL: How to display data if foreign key is implode (',')

I'm stuck on creating MySQL queries. I have two tables, which are user and factory. The data and structure are as below:
table user
enter image description here
table factory
enter image description here
From table user, u can see example, id = 1 got factoryID = 2,3. At table factory, id = 2 is F1 and id = 3 is F2.
Now, how i want to join the table, and display the data like example,
user.id = 1
user.name = Amira
user.factoryID = 2,3
factory.factoryName = F1,F2
Can anyone know how to write the query?
I suggest fixing your table design. Using FIND_IN_SET can do the trick, but you will be facing some performance issues, especially for larger data.
As per the question you could use:
select user.id,name,factoryID,group_concat(factoryName) as factoryName
from user
inner join factory ON FIND_IN_SET(factory.id,user.factoryID)
group by user.id,name,factoryID;
Result:
id name factoryID factoryName
1 Armira 2,3 F1,F2
2 Balqis 4,5 F3,F4
Demo

MYSQL - Removing number from One Table in SQL if it exists in another Table

I need some assistance with deleting data within an SQL Table if it matches data from another table.
There are two Tables
Table 1: DNC
Table 2: Call_Logs
Table 1 has only one column called phone_number.
Table 2 has multiple columns, but the main one that is important is also named phone_number.
Basically, I want to remove any numbers that are in Table 2 from Table 1, if they exist. Now, I don't want to delete every number from Table 1 if they exist in Table 2. What numbers I collect from Table 2 are based on some criteria.
To pull the data from Table 2 that I need to delete from Table 1, I use the following:
select phone_number from call_logs where call_date < 'DATE' and Status = 'DNC'
This query will give me a list of all phone numbers that I would want to remove from Table 1 if it exists.
EXAMPLE:
https://drive.google.com/file/d/0B4NE4ZDXd6steW5odWhBMDJSY1U/view
I am not sure how I would go about running the query in SQL. Any types would be appreciated it.
Looking to your sample in img
You could use a left join on table 2 (where table2.phone_number is null alias don't match)
delete from table1
left join table2 on table1.phone_number = table2.phone_number
where table2.phone_number is null
correlated subquery w/ an exists so it can early exit
The select 1 in the subquery is because we have to select a value but it doesn't matter what value that is. since the coloration (DNC.Phone_Number = CL.Phone_Number) is all we are after; along with your limits on call_log.
DELETE
FROM DNC
WHERE exists (SELECT 1
FROM Call_logs CL
WHERE CL.call_date < 'DATE'
and CL.Status = 'DNC'
and DNC.Phone_Number = CL.Phone_Number)

Differences between two result sets MySQL

Very beginner question but haven't been able to come up with answer after reading various help resources.
I have a table group_affiliations which is a joining table between the tables users and groups. Relevant columns: Id, user_id, group_id. I am doing a data cleanup where users were assigned a group_id based on a location which used to be a 3 character abbreviation of a city but has since gone to spelling out full city (ex: a group_id for CHA was previously assigned and now a group_id for Charlotte). Most users currently have both group_ids associated with their user_id but some still only have the old group_id and were never assigned the new one.
What is the most efficient way of finding which ids are in this result set:
select user_id from group_affiliations where group_id=OldId;
and not in this result set:
select user_id from group_affiliations where group_id=NewId;
SELECT 'user_id'
from 'group_affiliations'
where 'group_id' = OldId
and 'group_id' != NewId
how about using a JOIN
SELECT g1.'user_id'
from 'group_affiliations' g1
inner join 'group_affiliations' g2
on g2.'group_id' != NewId
and g2.'group_id' = OldId
and g1.'user_id'=g2.'user_id'

How to: Find and update all the entries where the value in one column shows up more than once

I have a table with the following columns:
subid - id of the resource
authorid - id of the author
ordering - order of author within citation
For an application where users can submit resources and cite multiple authors. Authors can cite primary and secondary authors in their submissions and usually do.
There is one case where a user (call him user 111) submitted all entries listing himself as the primary and the actual author as secondary. Unfortunately that person has left the project so it has fallen to me to fix this (I have to do it purely in sql).
I am trying to figure out how to build a query to do the following:
Find all entries
where the subid value shows up more than once in the table
where at least one of the authorid values is 111
where the ordering for 111 is greater than the ordering for any users that are not 111
& update them so
the not(111) author has ordering of '0'
and the 111 author has ordering '1'
Try this solution:
UPDATE tbl a
INNER JOIN
(
SELECT subid
FROM tbl
GROUP BY subid
HAVING COUNT(*) > 1 AND SUM(author_id = 111) > 0
) b ON a.subid = b.subid
SET a.ordering = (a.author_id = 111)
Replace tbl with your actual table name.

custom made order by mysql

I have a row of entities stored in a mysql table;
entity table
order
id
name
type (refers to Type(id))
and a row of types stored in another table
Type table
id
name
order
the ORDER column in Type table specifies by what order the entities should be sorted - some should be ordered by name and some by id.
How do I create a mysql query that gets the ORDER BY clause from the type table and sorts the entities in the entity table by the ORDER stored for that entity type in the Type table
for example, let us say I have the following rows:
Entity table:
row 1:
id = 1
name = Virginia
type = 1
row 2:
id = 2
name = Virginia
type = 1
row 3:
id = 3
name = Canada
type = 2
types (rows in Type table)
row 1
id = 1
name = states
order = "name"
row 2:
id = 2
name = countries
order = id
I want to do the following query
SELECT entities.id, entities.name FROM entities INNER JOIN type ON entities.type = type.id ORDER BY ....
in the ORDER BY I want to order the entities based on what is stored in the ORDER row in the type table. So countries should be sorted by Entity(ID) and states should be sorted by Entity(name). How can I do that?
This doesn't seem like a very good design for a database. For your example, I would suggest something more similar to this:
CREATE TABLE countries (
countryID INT NOT NULL,
countryName VARCHAR(30)
);
CREATE TABLE states (
stateID INT NOT NULL,
countryID INT,
stateName VARCHAR(30)
);
Then you can perform queries like:
SELECT c.countryName, s.stateName
FROM countries c LEFT JOIN states s ON c.countryID = s.countryID
ORDER BY countryName, stateName;
At the very least, I would suggest using more obvious names for your columns, like in your entity table, you have a column named 'type' which refers to the 'id' field in the type table. Perhaps name them both typeID or something more obvious.
I also think it's a bad idea to create a column that stores information about which column to order by. Not only does that mean that you'll have to execute two queries every time (one to fetch the order by, and one to fetch the actual data), but you will also be storing a lot of extra data unnecessarily.