SQL select query taking long time - php

I am using the below SQL query but it takes more than 180 sec to execute. Is there a way to speed it up ? This SQL give me the pic_id and of all the females.
SELECT pic_id, small
FROM picture
WHERE hide =0
AND userhide =0
AND `fbid`
IN (
SELECT fbid
FROM user
WHERE gender = "female"
)
ORDER BY `picture`.`pic_id` ASC
LIMIT 1500 , 200
The Explain SQL
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY picture index NULL PRIMARY 4 NULL 1700 Using where
2 DEPENDENT SUBQUERY user ALL NULL NULL NULL NULL 7496 Using where
--- Result of explain statement for Tim's sql answer --
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE picture ALL NULL NULL NULL NULL 41443 Using where; Using temporary; Using filesort
1 SIMPLE user ALL NULL NULL NULL NULL 7501 Using where; Using join buffer
-- Structure ---
CREATE TABLE `user` (
`sid` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 NOT NULL,
`first_name` varchar(255) CHARACTER SET utf8 NOT NULL,
`username` varchar(255) CHARACTER SET utf8 NOT NULL,
`birthday` date NOT NULL,
`location` varchar(255) CHARACTER SET utf8 NOT NULL,
`gender` varchar(6) CHARACTER SET utf8 NOT NULL,
`created` date NOT NULL,
`fbid` bigint(50) NOT NULL,
`token` varchar(255) CHARACTER SET utf8 NOT NULL,
`relationship_status` varchar(20) CHARACTER SET utf8 NOT NULL,
`smallest` varchar(255) CHARACTER SET utf8 NOT NULL,
`email` varchar(40) CHARACTER SET utf8 NOT NULL,
`ref` varchar(15) NOT NULL,
PRIMARY KEY (`sid`),
KEY `gender` (`gender`),
KEY `fbid` (`fbid`)
) ENGINE=MyISAM AUTO_INCREMENT=7595 DEFAULT CHARSET=latin
---- structure of picture table ---
CREATE TABLE `picture` (
`fbid` bigint(50) NOT NULL,
`pic_id` int(11) NOT NULL AUTO_INCREMENT,
`pic_location` varchar(255) NOT NULL,
`hide` int(1) NOT NULL,
`small` varchar(255) NOT NULL,
`userhide` int(1) NOT NULL,
`likes` int(10) NOT NULL,
`hot` int(1) NOT NULL,
PRIMARY KEY (`pic_id`),
UNIQUE KEY `pic_location` (`pic_location`),
UNIQUE KEY `small` (`small`),
KEY `fbid` (`fbid`),
KEY `hide` (`hide`),
KEY `userhide` (`userhide`)
) ENGINE=MyISAM AUTO_INCREMENT=42749 DEFAULT CHARSET=latin1

try something like this:
SELECT pic_id, small
FROM picture
INNER JOIN user ON ( picture.fbid = user.fbid and user.gender='female' )
WHERE hide =0
AND userhide =0
ORDER BY `picture`.`pic_id` ASC
LIMIT 1500 , 200
I put gender in the join because a query will not return rows that don't have a match on an inner join.
You should also read this stack overflow topic
EDIT:
make sure you have indexed the following fields:
picture.fbid
user.fbid
user.gender
picture.hide
picture.userhide

Try using a join instead:
SELECT p.pic_id, p.small
FROM picture p
INNER JOIN fbid f USING ( fbid )
WHERE p.hide =0
AND p.userhide =0
AND f.gender = 'female'
ORDER BY `picture`.`pic_id` ASC
LIMIT 1500 , 200

Related

PIVOT table mysql with data from multiple tables

I have a school management system, this system has to analyze data, especially student marks.
I want to be able to show student performance, in the following way;
Show a table that shows the students marks per subject as well as the student average mark and position.
Example
Position | Class | Student Average | Student Name | Geography | History | Biology | English | Math |
10 Grade11 57% John Doe 59% 40% 66% 48% 56%
11 Grade11 56% John Smith 53% 33% 56% 68% 26%
12 Grade11 55% Paul Doe 29% 30% 46% 38% 36%
This is the code I am using and it is not displaying the marks like above
SELECT
marks.student_id,
subjects.subject_name,
ROUND(AVG(mark)) AS mark
FROM
marks
INNER JOIN teaching_loads ON teaching_loads.id = marks.teaching_load_id
INNER JOIN subjects ON subjects.id = teaching_loads.subject_id
INNER JOIN grades ON grades.id=teaching_loads.class_id
WHERE
grades.stream_id = 5 AND marks.assessement_id = 1
GROUP BY
subject_id, marks.student_id
Result of above query is
The system has the following tables
Marks Table-This is the table that stores student marks
CREATE TABLE `marks` (
`id` bigint(20) UNSIGNED NOT NULL,
`teacher_id` bigint(20) UNSIGNED NOT NULL,
`student_id` bigint(20) UNSIGNED NOT NULL,
`teaching_load_id` bigint(20) UNSIGNED NOT NULL,
`assessement_id` bigint(20) UNSIGNED NOT NULL,
`mark` int(11) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Teaching Loads Table
This table is where we store the subject the teacher is teaching and in which class he/she is teaching that subject.
CREATE TABLE `teaching_loads` (
`id` bigint(20) UNSIGNED NOT NULL,
`teacher_id` bigint(20) UNSIGNED NOT NULL,
`subject_id` bigint(20) UNSIGNED NOT NULL,
`class_id` bigint(20) UNSIGNED NOT NULL,
`session_id` bigint(20) UNSIGNED NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Subjects Table
CREATE TABLE `subjects` (
`id` bigint(20) UNSIGNED NOT NULL,
`subject_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`subject_type` enum('core','elective','non-value','passing_subject') COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Users Table
CREATE TABLE `users` (
`id` bigint(20) UNSIGNED NOT NULL,
`name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,,
`password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`lastname` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`middlename` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
You'll have to join the marks, teaching_loads, and subjects tables multiple times, once for each subject, and calculate the row average on each row (The AVG aggregate function calculates a value across rows for the specified column). Here's an example with the first two subjects to show what I mean (this assumes all subjects for a given student are the same class):
SELECT
geog_load.class_id
,(geog.mark + hist.mark) / 2
,student.name || ' ' || student.lastname
,geog.mark
,hist.mark
FROM
users AS student
INNER JOIN marks AS geog ON geog.student_id = student.id
INNER JOIN teaching_loads AS geog_load ON geog_load.id = geog.teaching_load_id
INNER JOIN
subjects as geog_subject ON geog_subject.id = geog_load.subject_id
AND geog_subject.subject_name = 'Geography'
INNER JOIN marks AS hist ON hist.student_id = student.id
INNER JOIN teaching_loads AS hist_load ON hist_load.id = hist.teaching_load_id
INNER JOIN
subjects as hist_subject ON hist_subject.id = hist_load.subject_id
AND hist_subject.subject_name = 'History'
Then you can filter how you like with a WHERE clause, and use RANK() to show Position numbers, or whatever.

Optimize this query for 1000000+ rows

I need to pull the data and write it to a csv file but its taking too much time and too much ram. What is wrong with it and what can I do? Also, I feel like there's a redundancy in the query itself. I'm doing this with PHP.
Here's the query
CREATE TEMPORARY TABLE temp1 SELECT * FROM vicidial_closer_log
USE INDEX(call_date)
WHERE call_date BETWEEN '1980-01-01 00:00:00' AND '2019-03-12 23:59:59';
CREATE TEMPORARY TABLE temp2 SELECT * FROM vicidial_closer_log
USE INDEX(call_date)
WHERE call_date BETWEEN '1980-01-01 00:00:00' AND '2019-03-12 23:59:59';
SELECT a.call_date,
a.lead_id,
a.phone_number
AS customer_number,
IF(a.status != 'DROP', 'ANSWERED', 'UNANSWERED')
AS status,
IF(a.lead_id IS NOT NULL, 'inbound', 'outbound')
AS call_type,
a.USER
AS agent,
a.campaign_id
AS skill,
NULL
AS campaign,
a.status
AS disposition,
a.term_reason
AS Hangup,
a.uniqueid,
Sec_to_time(a.queue_seconds)
AS time_to_answer,
Sec_to_time(a.length_in_sec - a.queue_seconds)
AS talk_time,
Sec_to_time(a.park_sec)
AS hold_sec,
Sec_to_time(a.dispo_sec)
AS wrapup_sec,
From_unixtime(a.start_epoch)
AS start_time,
From_unixtime(a.end_epoch)
AS end_time,
c.USER
AS
transfered,
a.comments,
IF(a.length_in_sec IS NULL, Sec_to_time(a.queue_seconds),
Sec_to_time(a.length_in_sec + a.dispo_sec))
AS duration,
Sec_to_time(a.length_in_sec - a.queue_seconds + a.dispo_sec)
AS handling_time
FROM temp1 a
left outer join temp2 c
ON a.uniqueid = c.uniqueid
AND a.closecallid < c.closecallid
GROUP BY a.closecallid
I've uploaded screenshot of table structure and the indices.
Table Structure
Indices of Table
Thanks.
UPDATE:
SHOW CREATE TABLE vicidial_closer_log
vicidial_closer_log CREATE TABLE `vicidial_closer_log` (
`closecallid` int(9) unsigned NOT NULL AUTO_INCREMENT,
`lead_id` int(9) unsigned NOT NULL,
`list_id` bigint(14) unsigned DEFAULT NULL,
`campaign_id` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`call_date` datetime DEFAULT NULL,
`start_epoch` int(10) unsigned DEFAULT NULL,
`end_epoch` int(10) unsigned DEFAULT NULL,
`length_in_sec` int(10) DEFAULT NULL,
`status` varchar(6) COLLATE utf8_unicode_ci DEFAULT NULL,
`phone_code` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
`phone_number` varchar(18) COLLATE utf8_unicode_ci DEFAULT NULL,
`user` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`comments` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`processed` enum('Y','N') COLLATE utf8_unicode_ci DEFAULT NULL,
`queue_seconds` decimal(7,2) DEFAULT 0.00,
`user_group` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`xfercallid` int(9) unsigned DEFAULT NULL,
`term_reason` enum('CALLER','AGENT','QUEUETIMEOUT','ABANDON','AFTERHOURS','HOLDRECALLXFER', 'HOLDTIME','NOAGENT','NONE','MAXCALLS') COLLATE utf8_unicode_ci DEFAULT 'NONE',
`uniqueid` varchar(20) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`agent_only` varchar(20) COLLATE utf8_unicode_ci DEFAULT '',
`queue_position` smallint(4) unsigned DEFAULT 1,
`called_count` smallint(5) unsigned DEFAULT 0,
`nopaperform` varchar(5) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'NO',
`park_sec` int(3) DEFAULT 0,
`dispo_sec` int(3) DEFAULT 0,
`record_file` text COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`closecallid`),
KEY `lead_id` (`lead_id`),
KEY `call_date` (`call_date`),
KEY `campaign_id` (`campaign_id`),
KEY `uniqueid` (`uniqueid`),
KEY `phone_number` (`phone_number`),
KEY `date_user` (`call_date`,`user`),
KEY `closecallid` (`closecallid`)
) ENGINE=MyISAM AUTO_INCREMENT=1850672 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
EXPLAIN QUERY(On third query only):
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE a ALL NULL NULL NULL NULL 664640 Using temporary; Using filesort
1 SIMPLE c ALL NULL NULL NULL NULL 662480 Using where; Using join buffer (flat, BNL join)
UPDATE(Updated Query):
SELECT a.call_date,
a.lead_id,
a.phone_number
AS customer_number,
IF(a.status != 'DROP', 'ANSWERED', 'UNANSWERED')
AS status,
IF(a.lead_id IS NOT NULL, 'inbound', 'outbound')
AS call_type,
a.user
AS agent,
a.campaign_id
AS skill,
NULL
AS campaign,
a.status
AS disposition,
a.term_reason
AS Hangup,
a.uniqueid,
Sec_to_time(a.queue_seconds)
AS time_to_answer,
Sec_to_time(a.length_in_sec - a.queue_seconds)
AS talk_time,
Sec_to_time(a.park_sec)
AS hold_sec,
Sec_to_time(a.dispo_sec)
AS wrapup_sec,
From_unixtime(a.start_epoch)
AS start_time,
From_unixtime(a.end_epoch)
AS end_time,
c.user
AS
transfered,
a.comments,
IF(a.length_in_sec IS NULL, Sec_to_time(a.queue_seconds),
Sec_to_time(a.length_in_sec + a.dispo_sec))
AS duration,
Sec_to_time(a.length_in_sec - a.queue_seconds + a.dispo_sec)
AS handling_time
FROM vicidial_closer_log a
LEFT OUTER JOIN vicidial_closer_log c
ON a.closecallid <> c.closecallid
AND a.uniqueid = c.uniqueid
AND a.closecallid < c.closecallid
WHERE a.call_date BETWEEN '2018-01-01 00:00:00' AND '2019-03-13 23:59:59'
EXPLAIN on updated query:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE a ALL call_date,date_user NULL NULL NULL 662829 Using where
1 SIMPLE c ref PRIMARY,uniqueid,closecallid uniqueid 62 aastell_bliss.a.uniqueid 1 Using where
Updated Query Execution Result:
Number of rows present between given time range: 155016 rows
Time taken: 0.0149 secs
It works!
Summary of comments that lead to an answer:
CREATE TEMPORARY TABLE ... SELECT doesn't create indexes on the temporary table
Explicit use of a temporary table, particularly of a large size, will rarely give a performance gain.
Using table aliases in a join allows for a self join
Group by Primary Key on the left side of a join doesn't add much as its already unique and the JOIN had no aggregate expressions. GROUP BY adds an implicit ORDER BY so you expression could end up slower if a secondary index was used to join the table.
While the date range of the query was large, preparing for it to be a significant filter when small would make the call_date more favourable as an index. To make this more favorable, the join key is added to the end of the index so most of the work of the join can happen by just looking at the index.
When PK is on a column, a secondary index on the same column isn't needed.

inner join query selecting from database where lang is en

Hello i have one query with inner join that take a question and 4 answers from the db. I want to make this query to take only the questions that lang table is 'en'
This is the query:
$mysql->query("SELECT Q.id AS id, Q.question, QA.answer1, QA.answer2,
QA.answer3, QA.answer4, QA.correct, QC.name AS cat_name
FROM question Q
INNER JOIN question_answers QA ON QA.questionFK=Q.id
INNER JOIN question_cats QC ON QC.id=Q.categoryFK
ORDER BY rand()
LIMIT 1");
I try to make it like this:
$mysql->query("SELECT Q.id AS id, Q.question, QA.answer1, QA.answer2,
QA.answer3, QA.answer4, QA.correct, QC.name AS cat_name
FROM question Q
INNER JOIN question_answers QA ON QA.questionFK=Q.id
INNER JOIN question_cats QC ON QC.id=Q.categoryFK
WHERE Q.lang='en'
ORDER BY rand()
LIMIT 1");
But it didn't work, it's select everyting...
Where am i wrong and how should i make it?
Those are the 2 tables:
CREATE TABLE IF NOT EXISTS `question` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`question` varchar(255) NOT NULL,
`cnt` int(10) NOT NULL DEFAULT '0',
`correct` int(10) NOT NULL DEFAULT '0',
`categoryFK` int(3) NOT NULL DEFAULT '0',
`from_userFK` int(10) NOT NULL DEFAULT '0',
`correct_points` int(10) DEFAULT NULL,
`ut` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`del` enum('yes','no') NOT NULL DEFAULT 'no',
`lang` varchar(32) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=26 ;
CREATE TABLE IF NOT EXISTS `question_answers` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`questionFK` int(10) NOT NULL,
`answer1` varchar(255) CHARACTER SET cp1251 NOT NULL,
`answer2` varchar(255) CHARACTER SET cp1251 NOT NULL,
`answer3` varchar(255) CHARACTER SET cp1251 NOT NULL,
`answer4` varchar(255) CHARACTER SET cp1251 NOT NULL,
`correct` int(1) NOT NULL,
`ut` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`del` enum('yes','no') CHARACTER SET cp1251 NOT NULL DEFAULT 'no',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=25 ;
To me it feels like your filer condition WHERE Q.lang='en' doesn't matches any record and so the outcome. Consider trimming it before comparing like
WHERE TRIM(Q.lang) ='en'

Mysql query to sort and merge two tables

I have two tables in my mysql database called forum_topics and forum_replies.
I am looking to have an overview of the latest posts / replies added, like this: http://prntscr.com/6ixtz4
to do so, i need a way to makee it sort by the time in both tables, and if a forum_reply is in the result it need to get its topic from the forum_topics.
How can i make this work? I have no mysql query to referer to to make this work.
CREATE TABLE IF NOT EXISTS `forum_topics` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`topic` varchar(255) NOT NULL,
`thread` varchar(255) NOT NULL,
`level` int(2) NOT NULL DEFAULT '0',
`creator` int(255) NOT NULL,
`time` varchar(255) NOT NULL,
`innlegg` text NOT NULL,
`ip` varchar(255) NOT NULL,
`locked` enum('yes','no') NOT NULL DEFAULT 'no',
`deleted` enum('yes','no') NOT NULL DEFAULT 'no',
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
CREATE TABLE IF NOT EXISTS `forum_replies` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`topicid` int(255) NOT NULL,
`userid` int(255) NOT NULL,
`time` int(255) NOT NULL,
`reply` text NOT NULL,
`deleted` enum('yes','no') NOT NULL DEFAULT 'no',
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;
exampledata:
in table forum_topics
id |topic| time|
1 | test | 10
2 | test2 | 29
in table forum replies:
id | topicid | time |
1 | 1 | 18
2 | 2 | 28
As in this example i would like the outcome to be sorted something like this:
10
18
28
29
if forum_replies is one of the results it would need some data from forum_topics, but if forum_topics is one of the results it wouldnt need any data from the forum_replies.
The topicid is the id from the forum_topics of the topic that the reply is a response to.
So in the end i want to create the example output as in the image.
EDIT:
it would be a result containing in following order:
10
18
28
29
By loading the data in the example data sets, this will get the time from both tables, but sort the time from both as if they were in one table. The below query gets the results, and then orders them in order of time. Since both tables have the same structure, you can "union" them together as if they were one table and sort by time using "order_by"
select time
from forum_topics
UNION
select time
from forum_replies
order by time
Note: I was able to load both tables in the above request but without the last line on each. In other words I trimmed the part referring to the engine, and then just created the tables, and then entered the data provided..
I'm sorry I have to say the table structure of a forum post is incorrect. In your case, you will be very hard to sort a post based on topic and reply. I would suggest a table structure like this:
CREATE TABLE IF NOT EXISTS `forum_topics` (
`tid` int(255) NOT NULL AUTO_INCREMENT, // Topic ID
`tsubject` varchar(255) NOT NULL, // Topic Subject
`level` int(2) NOT NULL DEFAULT '0',
`creator` int(255) NOT NULL,
`innlegg` text NOT NULL, // I don't know what this means ...
`locked` enum('yes','no') NOT NULL DEFAULT 'no',
`deleted` enum('yes','no') NOT NULL DEFAULT 'no',
PRIMARY KEY (`tid`),
UNIQUE KEY `id` (`tid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5;
CREATE TABLE IF NOT EXISTS `forum_posts` (
`pid` int(255) NOT NULL AUTO_INCREMENT,
`tid` int(255) NOT NULL,
`userid` int(255) NOT NULL,
`time` int(255) NOT NULL,
`pcontent` text NOT NULL, // Post Content
`isauthorpost` int(1) NOT NULL, // Optional: If this is the first post of the topic
`deleted` enum('yes','no') NOT NULL DEFAULT 'no',
PRIMARY KEY (`pid`),
UNIQUE KEY `id` (`pid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;
So now you will have a topic id to follow by the first. And you can just search for all replies with ORDER BY pid, for the topic details, you can use LEFT JOIN forum_topics or INNER JOIN to finish.

PHP/MYSQL - Which Join Statement To Use To Simplify My Queries?

I have these 2 queries and i would like to join them into one but i am unsure of how to go about it.
Query 1:
$query = "SELECT * FROM ".$db_tbl_comics." WHERE ".$db_fld_comics_publisher."='".$pub_id."'
AND ".$db_fld_comics_active."='1' GROUP BY ".$db_fld_comics_arc;
Query 2:
$q2 = mysql_query('SELECT '.$db_fld_arcs_title.' FROM '.$db_tbl_arcs.'
WHERE '.$db_fld_arcs_id.'="'.$result[$db_fld_comics_arc].'"');
Comics Table:
CREATE TABLE IF NOT EXISTS `comics` (
`id` varchar(255) NOT NULL,
`arc` int(255) NOT NULL,
`title` varchar(255) NOT NULL,
`issue` decimal(5,1) DEFAULT NULL,
`price` decimal(10,2) NOT NULL,
`plot` longtext NOT NULL,
`publisher` int(255) NOT NULL,
`isbn` varchar(255) NOT NULL,
`published` date NOT NULL,
`cover` varchar(255) NOT NULL DEFAULT './images/nopic.jpg',
`added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`views` int(255) NOT NULL DEFAULT '0',
`active` int(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `arc` (`arc`,`title`,`issue`,`publisher`)
);
Arcs Table:
CREATE TABLE IF NOT EXISTS `arcs` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`plot` longtext NOT NULL,
`added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `title` (`title`)
);
What I need to do is get the Arcs Title from the arcs table for the respective comic arc.
You need to use INNER JOIN for that since I presume that records are present on both tables.
SELECT a.*, b.title
FROM comics a INNER JOIN arcs b
on a.id = b.id
WHERE a.Title = 'VALUEHERE'
displays all details from comics table and the title of the arc
as simple as (joining 2 queries in one, by selecting only the required field and using IN):
SELECT
'.$db_fld_arcs_title.'
FROM '.$db_tbl_arcs.'
WHERE '.$db_fld_arcs_id.' IN (
SELECT '.$db_fld_comics_arc.'
FROM '.$db_tbl_comics.'
WHERE '.$db_fld_comics_publisher.'='".$pub_id."'
AND '.$db_fld_comics_active.'='1' GROUP BY '.$db_fld_comics_arc.'
)

Categories