How to compare the one table elements to another table - php

Using below query I am getting the last record which is updated in the table using update_date
SELECT form_id,form_elements,form_builder_type, update_date FROM `form_builder` WHERE form_id=1 AND form_builder_type='example' ORDER BY update_date DESC LIMIT 1
Output is below
Now I have the second table with field name and field type
I have to check the form_elements which are coming from the first table is available or not in the second table.
If available in the table then display the field name and field type
I need an output like
lastname: text
email : email
Would you help me out in this?

will not tell you that saving data like you do is not correct way to do things, so:
SELECT t.id, t.form_builder_type, e.field_type FROM form_builder t
LEFT JOIN form_elements e
ON e.field_name regexp concat('(',replace(e.form_elements, ',','|'),')');
Hope you'll use it to update your scheme ;)

Related

How do i display the information of foreign key instead of id in MySQL and PHP?

just like the caption, how do i fetch information from foreign key instead of id?
Here are the first table:
and here are the second table:
i want to display product_name and unit_price instead of id, how to do it?
You need to use join
Your query should look something like this:
SELECT ft.id, pr.product_name, pr.unit_price from firsttable as ft join product as pr on ft.product_id = pr.product_id;
Where firsttable is the table of the first screenshot and product is the table from the second screenshot.
I don't know what you need from the first table so I just took a column id, however I do not know if you have this column. Here you just put whatever you need from the first table.

Select from multiple tables using union all and single order by ,php

I ran into an error I cant seem to come out of.
am not too good with unions
I want to loop through 4 different tables(using union all) and manipulate their values to fit my needs.
I also need to use single 'ORDER by Date DESC' (Date are integer values) for the whole union all, so that I can arrange the output in a pattern,
when I add the 'order by date desc ' to it, code doesn't work . and when I remove it , the values of the second query are attached to the names of the first query, am sooo confused.
I tried "Select * from table_name where..... it idnt work in this case , that's why I had to bring out all table_names I need to the query,
Basically , I want to echo each value from the query uniquely when I need to,
any help is appreciated, thanks
<?php
$q114="(SELECT id AS id1,text_post AS text_post1,likes AS likes1
FROM timeline_posts WHERE email='$owner_email')
UNION ALL (SELECT pic_comment AS pic_comment2, comments AS comments2, date AS date2
FROM pictures WHERE email='$owner_email')
UNION ALL (SELECT image AS image3,likes AS likes3, comments AS comments3
FROM profile_pics WHERE email='$owner_email')
UNION ALL (SELECT likes AS likes4, comments AS comments4, date AS date4
FROM friends_timeline_post WHERE timeline_email='$owner_email')
ORDER BY 'date' DESC";
$pages_query=mysqli_query($connect,$q114);
while($fetch9=mysqli_fetch_assoc($pages_query))
{
print_r($fetch9['likes3'] );
//a lot of work to be done here
}
?>
For "unioning" the results of multiple selects and ordering those results by a column name across all the results of the multiple selects, column names must be the same in all selects.
You could add a column to all selects that would contain your "digit" in order to still be able to distinguish between let say "likes1" and "likes2" even if their column name is "likes" for all the selects that you "unioned".

MySQL Query showing only the last item grouped by user

I got a database that registers user actions and their geolocation.
Now I would like to fetch this data at the hand of the last action per user.
The table looks a bit like:
geoaction_id AUTO INCREMENT
geoaction_user
geoaction_creationdate (Y-m-d H:i:s)
geoaction_action
geoaction_lon
geoaction_lat
Now I would like to make a simple query that selects of all users the last item.
But LIMIT 0,1 just parses one row no matter what. (LOGICALLY!!)
Group by gives a little better result.
But how to get only the last item per user?
Try this, please provide the queries you have checked out so far, in order to assist you better.
SELECT geoaction_user, geoaction_action
FROM table-name
GROUP BY geoaction_user
ORDER BY geoaction_action DESC LIMIT 1
Working with sets:
SELECT
g.geoaction_user,
g.geoaction_action,
g.geoaction_creationdate,
g.geoaction_lat,
g.geoaction_lon
FROM
(
SELECT
geoaction_user,
MAX(geoaction_id) max_id
FROM
geoactions
GROUP BY geoaction_user
) s
JOIN
geoactions g
ON s.geoaction_user = g.geoaction_user
AND s.max_id = geoaction_id
The subquery generates a virtual table with the geoaction_id from the latest entry in the tabble for each user_id, then the table is joined to get the data belong to the latest id.
If you need to filter out some records place the where clause in the subquery

MySQL Procedure (help)

I have several tables in my database such as
comments
status
events
I’m trying to create an SQL query procedure which counts data from these different tables based on the userID entered and then sum up the counts to create a unique valued. This is what i’ve tried so far but i’m having problems with the syntax. Where am I going wrong??
SELECT COUNT(user_id) AS comments FROM comment
WHERE user_id= userID
UNION ALL
SELECT COUNT(creator_id) AS events FROM event
WHERE creator_id=userID;
In a union, the fields are combined based on order. So giving the count field a different name in each part of the union does not make two fields. It becomes the same field in the end. To differentiate which value came from which table, add a hardcoded string literal like so:
SELECT COUNT(user_id) AS rows, 'comment' as tablename FROM comment
WHERE user_id= userID
UNION ALL
SELECT COUNT(creator_id) AS rows, 'event' as tablename FROM event
WHERE creator_id=userID;

How do I efficiently select unique row/record data from 2 mysql tables and return one merged result, ordered by timestamp?

Stack:
I'm trying to turn my website's user profile into more of a feed style page. Currently the profile shows some user stats at the top, then the last 10 comments a user submitted (ordered by timestamp descending), then the last 10 posts they have submitted.
What I want to do instead, is have the last 20 "actions" (either comment or post submission) listed in order of the timestamp (so the comments and submissions will be merged together in the list instead of having 2 seperate lists). You know, like a "feed."
The issue is that the comments are pulled from a comments table, and the submissions are pulled from a "submissions" table.
I've solved it in a pretty inefficient way by using a union query to select "comment_id/submission_id", "field the identifies record as a comment or a submission", "timestamp" from both tables.
This gives me a result that tells my the entity id, as well as defines the entity as a comment or a post, by which I can then shoot off another query in a while mysql_fetch_array statement to get the full comment or submission data.
This just seems really dirty to me, since I'm basically querying the tables, finding the rows/records that I need (but ignoring the actual data I need since the different table's columns don't match up as I believe to be necessary for a union query), then going back for the data I ignored the first time with individual queries in a while statement...
Is there a better way to do this that I don't know of?
Additional notes:
Example sql I'm currently using to build the initial result I spoke of above:
select comment_id, datatype, timestamp from comments where userid=3
union all
select content_id, datatype, timestamp from submissions where userid=3
ORDER BY timestamp DESC
Returns a result like this:
commentid datatype timestamp
5201 post 2012-03-27 20:30:40
43761 comment 2012-03-26 21:00:19
43759 comment 2012-03-26 20:59:47
5033 post 2012-03-26 20:57:36
43755 comment 2012-03-26 20:54:57
43745 comment 2012-03-26 16:32:24
Pseudocode I can then use to print out the information onto the profile page:
while ($content_array = mysql_fetch_array($abovequery)){
Individual select query fetching the full row by from either comment table or submission table by id depending on $content_array['datatype'];
echo out relevant data from individual select query onto profile screen in a pretty way;
}
Surely this can be done better?
If you are unable to coerce the columns of the two full queries in such a way that it can all be returned in the UNION then you could join the union result to the comments and submissions tables.
SELECT records.datatype, comments.*, submissions.*
FROM (
(
SELECT comment_id, datatype
FROM comments
WHERE userid=3
ORDER BY timestamp DESC
LIMIT 20
) UNION ALL (
SELECT content_id, datatype
FROM submissions
WHERE userid=3
ORDER BY timestamp DESC
LIMIT 20
)
ORDER BY timestamp DESC
LIMIT 20
) AS records
LEFT JOIN comments
ON records.comment_id = comments.comment_id
AND records.datatype = 'comment'
LEFT JOIN submissions
ON records.comment_id = submissions.content_id
AND records.datatype = 'post'
Alternatively, you could run the two innermost selects with all required fields and then order the final result in PHP.

Categories